如何在WPF中添加自定义路由命令?

我有一个包含菜单和子菜单的应用程序。 我已将Appliocation命令附加到某些子菜单项(如“剪切”,“复制”和“粘贴”)。
我也有一些没有应用程序命令的其他菜单项。
我怎么能添加一个自定义的命令绑定到这些子菜单项?
我已经通过这篇文章,但无法将事件附加到我的子菜单项。

我使用了一个静态类,放置在Window1类(或任何窗口类恰好被命名的地方)之后,我创build了RoutedUICommand类的实例:

public static class Command { public static readonly RoutedUICommand DoSomething = new RoutedUICommand("Do something", "DoSomething", typeof(Window1)); public static readonly RoutedUICommand SomeOtherAction = new RoutedUICommand("Some other action", "SomeOtherAction", typeof(Window1)); public static readonly RoutedUICommand MoreDeeds = new RoutedUICommand("More deeds", "MoreDeeeds", typeof(Window1)); } 

在窗口标记中使用Window1类所在的名称空间添加一个名称空间:

 xmlns:w="clr-namespace:NameSpaceOfTheApplication" 

现在我可以为命令创build绑定,就像应用程序命令一样:

 <Window.CommandBindings> <CommandBinding Command="ApplicationCommands.Open" Executed="CommandBinding_Open" /> <CommandBinding Command="ApplicationCommands.Paste" Executed="CommandBinding_Paste" /> <CommandBinding Command="w:Command.DoSomething" Executed="CommandBinding_DoSomething" /> <CommandBinding Command="w:Command.SomeOtherAction" Executed="CommandBinding_SomeOtherAction" /> <CommandBinding Command="w:Command.MoreDeeds" Executed="CommandBinding_MoreDeeds" /> </Window.CommandBindings> 

并使用菜单中的绑定例如:

 <MenuItem Name="Menu_DoSomething" Header="Do Something" Command="w:Command.DoSomething" /> 

不用在静态类中定义它们,也可以直接在XAML中声明命令。 例子(改编自Guffas很好的例子):

 <Window.Resources> <RoutedUICommand x:Key="DoSomethingCommand" Text="Do Something" /> <RoutedUICommand x:Key="DoSomethingElseCommand" Text="Do Something Else" /> </Window.Resources> <Window.CommandBindings> <CommandBinding Command="{StaticResource DoSomethingCommand}" Executed="CommandBinding_DoSomething" /> <CommandBinding Command="{StaticResource DoSomethingElseCommand}" Executed="CommandBinding_DoSomethingElse" /> </Window.CommandBindings> ... <MenuItem Name="Menu_DoSomething" Header="Do Something" Command="{StaticResource DoSomethingCommand}" /> 

我知道我的答案为时已晚 ,但我希望这对未来有所帮助。

我喜欢Guffa和Heinzi的回答,但是你只能用一个命令来实现以前的结果。 我通常使用帮助命令

  <Window.CommandBindings> <CommandBinding Command="{StaticResource Help}" Executed="HelpExecuted" /> </Window.CommandBindings> 

我每次使用CommandParametr例如

 <Window.InputBindings> <KeyBinding Command="{StaticResource Help}" Key="A" Modifiers="Ctrl" CommandParameter="Case1"/> <KeyBinding Command="{StaticResource Help}" Key="B" Modifiers="Ctrl" CommandParameter="Case2"/> <KeyBinding Command="{StaticResource Help}" Key="C" Modifiers="Ctrl" CommandParameter="Case3"/> <KeyBinding Command="{StaticResource Help}" Key="D" Modifiers="Ctrl" CommandParameter="Case4"/> <MouseBinding Command="{StaticResource Help}" MouseAction="LeftDoubleClick" CommandParameter="Case5" /> </Window.InputBindings> 

要么

 <Button Command="Help" CommandParameter="Case6" Content="Button"> <Button.InputBindings> <KeyBinding Command="{StaticResource Help}" Gesture="Ctrl+D" CommandParameter="Case7"/> </Button.InputBindings> </Button> 

并在cs文件中

 private void HelpExecuted(object sender, ExecutedRoutedEventArgs e) { string str = e.Parameter as string; switch (str) { case null://F1 Pressed default Help //Code break; case "Case1": //Code break; case "Case2": //Code break; case "Case3": //Code break; case "Case4": break; case "Case5": //Code break; case "Case6": //Code break; case "Case7": //Code break; } e.Handled = true; } 

如果你使用MVVM模式

 private void HelpExecuted(object sender, ExecutedRoutedEventArgs e) { string str = e.Parameter as string; Mvvm_Variable.Action(Input: str); e.Handled = true; } 

并将开关移动到ViewModule站点。 而Action是同一个ViewModule类中的一个方法。