Skip to main content

Creating mouse and keyboard shortcuts

Here is an example of how to bind shortcut keys commonly used in business tools, e.g. using a double-click or the Enter key in a simple list box.

<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>
Discussion

Have questions or feedback? Join the conversation below.