跳到主要内容
版本:11.0.0

KeyBinding and MouseBinding

  • This section explains how to place shortcut keys that often appear in business tools in various controls.
  • As an example, binding with double-click or the Enter key on a simple list box.
  • It also works for DataGrid.
<UserControl ..>
<StackPanel>
<ListBox
DoubleTapped="ListBox_DoubleTapped"
ItemsSource="{Binding OperatingSystems}"
SelectedItem="{Binding OS}">
<ListBox.KeyBindings>
<!-- Enter -->
<KeyBinding Command="{Binding PrintItem}" Gesture="Enter" />
<!--
MouseBindings are not supported.
Instead, handle it in the view's code-behind. (DoubleTapped event)
-->
</ListBox.KeyBindings>
</ListBox>
<TextBlock Text="{Binding Result}">
<TextBlock.ContextMenu>
<ContextMenu>
<!-- Right Click -->
<MenuItem Command="{Binding Clear}" Header="Clear" />
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</StackPanel>
</UserControl>