要做到這一點,最好的辦法是讓一個Command
,和InputGesture
與該命令相關聯:
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.
<MenuItem Header="_New" Command="{x:Static local:Commands.CreateNew}">
...
// Wherever you want to process the command. I am assuming you want to do it in your
// main window, but you can do it anywhere in the route between your main window and
// the menu item.
<Window.CommandBindings>
<CommandBinding Command="{x:Static local:Commands.CreateNew}"> Executed="CreateNew_Executed" />
</Window.CommandBindings>
...
// In the code behind for your main window (or whichever file you put the above XAML in)
private void CreateNew(object sender, ExecutedRoutedEventArgs e)
{
...
}
如果你真的只想要一個「新建」命令,就可以跳過創建RoutedCommand
和InputGesture
,因爲該命令是爲已創建:
<MenuItem Header="_New" Command="New">
...
<Window.CommandBindings>
<CommandBinding Command="New" Executed="New_Executed" />
</Window.CommandBindings>
...
private void New_Executed(object sender, ExecutedRoutedEventArgs e)
{
...
}