views:

42

answers:

2

In a WPF DataGrid the F2 key is used to edit current cell. The requirements in my software project says that F2 should do something else. I have added a keybinding, but it only works when keyboardfocus is not in the DataGrid.

Can I disable or remove the DataGrid's keybinding to the F2 key?

+1  A: 

How about setting the RoutedEventArgs.Handled property to true in the Keyboard.PreviewKeyDown attached event or in the UIElement.PreviewKeyDown routed event in case it was the F2 key.

bitbonk
I'll try it, but wouldn't that mean that the F2 never get bubbled up to the parents of the DataGrid, where my other F2-keybinding sits?
Guge
No you intercept it in the parent before it gets *tunneled* down to the DataGrid.
bitbonk
I put a decorator around the DataGrid and set Handeled to true in PreviewKeyDown. That stops the tunneling event, so the DataGrid cell doesn't go to edit mode, but the KeyDown event doesn't bubble up, so my Window inputbinding for F2 doesn't work either.
Guge
`RoutedEventArgs.Handled` doesn't go well with Input Keybindings. Instead of using a KeyBinding how about using the PreviewKeyDown event directly to execute your command (and set `e.Handled = true` thereafter)?
bitbonk
+2  A: 

I did not find a way to override the use of the F2 key in the DataGrid. I could disable the functionality in the DataGrid, but I couldn't at the same time hook it up to anything else.

It was easier to get the stakeholders to change the requirements. After all, most Windows users are familiar with F2 to enable editing, as it is in both Explorer and Excel.

Guge
+1 for a rare victory of platform-consistent sanity!
Greg D
@Greg. Thanks. Yes, it was like a ray of sunshine!
Guge