Mar 19, 2009 at 5:40 PM
Edited Mar 19, 2009 at 5:49 PM
Key input is necessarily focus-based. That means that if your control has an input binding you need to make sure that the control is focused in order for the binding to work. In
the particular case of a Button, placing your mouse over the button is not enough, you need to press the button in order for the button to gain focus. That is why buttons are not the best choice to put keyBindings. In most scenarios KeyBindings are placed
in the Window element or in a control that you are sure it's focused when you need it.
You might want to take a look at
Commanding Overview and
To assign a control a
KeyBinding you need to add it to its InputBindings.
For example if you want to execute a command when you press F2 or pressing a button, you can add a key binding to the window’s
InputBindings as follows:
Or the same can be accomplished in code:
If you want to put the binding in a userControl you can use the UserControl's
Focus method to set the focus on that control. You might find useful this MSDN article:
Hope it helps!