|
| 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="" /> |
0 commit comments