Enter key event in WPF datagrid

Jul 21, 2009 at 6:16 AM


Is there an event to track the keyboard's "Enter" key event in the WPF data grid?

In fact, I want to get the selected datagrid row when I  hit the "Enter" key.





Jul 21, 2009 at 7:47 AM

I don't think there is a direct way to do it. I solved a very similar issue (custom logic when certain keys were pressed while in edit mode) by changing the EditingElementStyle and adding a PreviewKeyDown event handler to the textbox in (actually a class deriving from) DataGridTextColumn. Maybe something similar can work for you as well.

Jul 21, 2009 at 10:21 AM

I have done it following way,

Added PreviewKeyDown event into Datagrid and inside that event I checked the key.

If the key is "Return" (Enter key) , then got the selected item from the grid.

Key can be get from, System.Windows.Input.KeyEventArgs. If you have entered the "Enter" key, value would be System.Windows.Input.Key.Return