Skip to content

Commit 9a95920

Browse files
Merge pull request #425 from rtazaki/main
Adding KeyBinding and MouseBinding
2 parents 5bd23b5 + 538cc27 commit 9a95920

File tree

4 files changed

+105
-6
lines changed

4 files changed

+105
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
---
2+
description: CONCEPTS - Input
3+
---
4+
5+
import Tabs from '@theme/Tabs';
6+
import TabItem from '@theme/TabItem';
7+
8+
import KeyMouseScreenshot from '/img/reference/controls/input/binding-key-mouse-test.gif';
9+
10+
# KeyBinding and MouseBinding
11+
- This section explains how to place shortcut keys that often appear in business tools in various controls.
12+
- As an example, binding with double-click or the Enter key on a simple list box.
13+
- It also works for DataGrid.
14+
15+
<Tabs
16+
defaultValue="xaml"
17+
values={[
18+
{ label: 'XAML', value: 'xaml', },
19+
{ label: 'CodeBehind', value: 'code-behind', },
20+
{ label: 'ViewModel', value: 'ViewModel', },
21+
]}
22+
>
23+
<TabItem value="xaml">
24+
25+
```xml
26+
<UserControl ..>
27+
<StackPanel>
28+
<ListBox
29+
DoubleTapped="ListBox_DoubleTapped"
30+
ItemsSource="{Binding OperatingSystems}"
31+
SelectedItem="{Binding OS}">
32+
<ListBox.KeyBindings>
33+
<!-- Enter -->
34+
<KeyBinding Command="{Binding PrintItem}" Gesture="Enter" />
35+
<!--
36+
MouseBindings are not supported.
37+
Instead, handle it in the view's code-behind. (DoubleTapped event)
38+
-->
39+
</ListBox.KeyBindings>
40+
</ListBox>
41+
<TextBlock Text="{Binding Result}">
42+
<TextBlock.ContextMenu>
43+
<ContextMenu>
44+
<!-- Right Click -->
45+
<MenuItem Command="{Binding Clear}" Header="Clear" />
46+
</ContextMenu>
47+
</TextBlock.ContextMenu>
48+
</TextBlock>
49+
</StackPanel>
50+
</UserControl>
51+
```
52+
53+
</TabItem>
54+
<TabItem value="code-behind">
55+
56+
```cs
57+
public partial class MainView : UserControl
58+
{
59+
public MainView()
60+
{
61+
InitializeComponent();
62+
}
63+
64+
private void ListBox_DoubleTapped(object? sender, Avalonia.Input.TappedEventArgs e)
65+
{
66+
if (DataContext is MainViewModel vm)
67+
{
68+
vm.PrintItem.Execute(null);
69+
}
70+
}
71+
}
72+
```
73+
</TabItem>
74+
75+
<TabItem value="ViewModel">
76+
77+
```cs
78+
public class MainViewModel : ViewModelBase
79+
{
80+
public List<string> OperatingSystems =>
81+
[
82+
"Windows",
83+
"Linux",
84+
"Mac",
85+
];
86+
public string OS { get; set; } = string.Empty;
87+
88+
[Reactive]
89+
public string Result { get; set; } = string.Empty;
90+
91+
public ICommand PrintItem { get; }
92+
public ICommand Clear { get; }
93+
94+
public MainViewModel()
95+
{
96+
PrintItem = ReactiveCommand.Create(() => Result = OS);
97+
Clear = ReactiveCommand.Create(() => Result = string.Empty);
98+
}
99+
}
100+
```
101+
</TabItem>
102+
</Tabs>
103+
104+
<img src={KeyMouseScreenshot} alt="" />

docs/concepts/input/hotkeys.md

-6
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@ A Hotkey must have one [Key](http://reference.avaloniaui.net/api/Avalonia.Input/
4444
IsVisible="False" />
4545

4646
<!-- These didn't work -->
47-
<!-- Button.KeyBindings -->
48-
<Button Command="{Binding CommandX}" Content="X">
49-
<Button.KeyBindings>
50-
<KeyBinding Gesture="Ctrl+1" />
51-
</Button.KeyBindings>
52-
</Button>
5347
<!-- Alt+Number -->
5448
<Button Command="{Binding CommandX}" Content="_1" />
5549
```

sidebars.js

+1
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ const sidebars = {
456456
'concepts/input/focus',
457457
'concepts/input/gestures',
458458
'concepts/input/hotkeys',
459+
'concepts/input/binding-key-and-mouse',
459460
],
460461
},
461462
'concepts/the-main-window',
Loading

0 commit comments

Comments
 (0)