In my WPF Datagrid I capture the "delete" key, process it, and then the datagrid itself deletes the row from the UI by continuing to process its own handler for the delete key (which is what I want).
But now I want CTRL-S to open up a search bar, which it does, but it also goes on to blank out the cell the user was on when he pressed CTRL-S, so I am looking for a way to tell the datagrid to cancel the key press so that it is not executed on the Datagrid.
How can I cancel a keypress like this?
XAML:
<toolkit:DataGrid x:Name="TheDataGrid" DockPanel.Dock="Bottom"
CanUserAddRows="False"
AlternatingRowBackground="#ddd"
CanUserSortColumns="true"
PreviewKeyDown="TheDataGrid_PreviewKeyDown"
AutoGenerateColumns="False"
RowEditEnding="TheDataGrid_RowEditEnding">
code-behind:
private void TheDataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (Keyboard.IsKeyDown(Key.LeftCtrl) && Keyboard.IsKeyDown(Key.S))
{
ShowSearchBar();
}
switch (e.Key)
{
case Key.Delete:
DeleteCustomer(sender, e);
break;
}
}
e.Handled = true;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With