Search code examples
c#wpfdatagrid

How to handle PreviewKeyDown event in DataGrid when the user has not selected any item. (WPF)


I can handle the PreviewKeyDown event when some items of a DataGrid are selected. But if users don't select any items, the event will not be triggered. Could I handle the PreviewKeyDown event in this case?


Solution

  • You can handle previewkeydown on the datagrid but something in the datagrid would need to have keyboard focus.

    Technically, that can happen without selecting a row.

    Even then, you might find a control in the datagrid handles the event.