I want to implement Keyboard shortcuts for a MenuItem
. I have used the code below:
<MenuItem Header="_New" InputGestureText="CTRL+N" Click="NewMenu_Click">
<MenuItem.Icon>
<Image Source= "Images\NEW.PNG" Width="25" Height="28" />
</MenuItem.Icon>
</MenuItem>`
But the InputGestureText
property is not responding when I pressed the CTRL+N
. I am using Visual Studio Express Edition 2010. Am I missing anything here?
One solution that doesn't involve commands and bindings is to override the owning Window's
OnKeyDown
method and search a menu item that has aKeyGesture
that matches the keyboard event.Here is the code for the Window's OnKeyDown override:
And here is the utility code that matches a menu item with the keyboard event:
It is quite explicit in the documentation for the property:
The best way to do this is to make a
Command
, and associate theInputGesture
with that command:If you really just want a "New" command, you can skip creating the
RoutedCommand
andInputGesture
, because that command is already created for you: