Reputation: 19396
I would like to know if someone has a somple example of how to use keybindings with a comboBox control, because I would like to use keybindings to capture the up arrow and down arrow keys.
Thanks.
Upvotes: 0
Views: 1096
Reputation: 26362
It is super simple to capture Keys in WPF. You simple add a PreviewKeyDown
event in WPF.
<Window x:Class="WpfApplication13.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ComboBox Name="myComboBox" PreviewKeyDown="MyComboBox_PreviewKeyDown"/>
</Grid>
</Window>
Then trigger something based on the event in the code-behind
.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
void MyComboBox_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Down)
{
MessageBox.Show("Key Down");
}
else if (e.Key == Key.Up)
{
MessageBox.Show("Key Up");
}
}
}
Upvotes: 3