In my WPF Datagrid, I grab the “delete” key, process it, and then the datagrid itself removes the row from the user interface, continuing to process its own handler for the delete key (this is what I want).
But now I want CTRL-S to open the search bar , which it does, but also continues to go out in the cell where the user was turned on when he pressed CTRL-S, so I'm looking for a way to tell datagrid to cancel the keypress so that It was not executed in Datagrid.
How can I cancel a keystroke as follows?
XAML:
<toolkit:DataGrid x:Name="TheDataGrid" DockPanel.Dock="Bottom" CanUserAddRows="False" AlternatingRowBackground="#ddd" CanUserSortColumns="true" PreviewKeyDown="TheDataGrid_PreviewKeyDown" AutoGenerateColumns="False" RowEditEnding="TheDataGrid_RowEditEnding">
background code:
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; } }
source share