Capturing Delete Key in WPF app

Aug 14, 2009 at 3:09 PM


We have a WPF application that uses lots of the technology but for one example where we are using a WPF Datagrid. If the user selects a row and presses our Delete button all is fine. When they select a row and press the keyboard Delete key, it deletes the row, but it hasn't been deleted from our object, of course. How are others dealing with this situation? Is there a way to catch the Delete keyboard press? Or is there some other solution possible?

Thanks in Advance!

Bill Campbell

Aug 15, 2009 at 8:17 AM
Edited Aug 15, 2009 at 8:25 AM

Hi Bill,

try this:


<dg:DataGrid Keyboard.PreviewKeyDown="dataGrid_PreviewKeyDown"> 


 private void dataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
            if (e.Key == Key.Delete)
                // Do something here....
                e.Handled = true;
Hope this helps...


Aug 16, 2009 at 9:37 AM

Thanks Jus - Appreciate it!