I want to implement Keyboard shortcuts for a MenuItem. I have used the code below:
The best way to do this is to make a Command, and associate the InputGesture with that command:
public static class Commands
{
public static readonly RoutedCommand CreateNew = new RoutedCommand("New", typeof(Commands));
static Commands()
{
SomeCommand.InputGestures.Add(new KeyGesture(Key.N, ModifierKeys.Control));
}
}
...
// Wherever you want to create the MenuItem. "local" should be the namespace that
// you delcared "Commands" in.
If you really just want a "New" command, you can skip creating the RoutedCommand and InputGesture, because that command is already created for you: