Capturing Delete Key in WPF app

Aug 14, 2009 at 4:09 PM

Hi,

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 9:17 AM
Edited Aug 15, 2009 at 9:25 AM

Hi Bill,

try this:

XAML:

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

Code:

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

Jus

Aug 16, 2009 at 10:37 AM

Thanks Jus - Appreciate it!