C# 如何在 WPF 中添加自定义路由命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/601393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I add a custom routed command in WPF?
提问by Sachin Gaur
I have an application that contains Menu and sub menus. I have attached Appliocation Commands to some of the sub menu items such as Cut, Copy and Paste.
I also have some other menu items that do not have application commands.
How could I add a custom command binding to those sub menu items?
I have gone through thisarticle but unable to attach event to my sub menu items.
我有一个包含菜单和子菜单的应用程序。我已将应用命令附加到一些子菜单项,例如剪切、复制和粘贴。
我还有一些其他没有应用程序命令的菜单项。
如何向这些子菜单项添加自定义命令绑定?
我已经阅读了这篇文章,但无法将事件附加到我的子菜单项。
采纳答案by Guffa
I use a static class that I place after the Window1 class (or whatever the window class happens to be named) where I create instances of the RoutedUICommand class:
我使用了一个静态类,它放在 Window1 类(或任何窗口类碰巧被命名的)之后,我在其中创建了 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));
}
Add a namespace in the window markup, using the namespace that the Window1 class is in:
在窗口标记中添加一个命名空间,使用 Window1 类所在的命名空间:
xmlns:w="clr-namespace:NameSpaceOfTheApplication"
Now I can create bindings for the commands just as for the application commands:
现在我可以像为应用程序命令一样为命令创建绑定:
<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>
And use the bindings in a menu for example:
并使用菜单中的绑定,例如:
<MenuItem Name="Menu_DoSomething" Header="Do Something" Command="w:Command.DoSomething" />
回答by Heinzi
Instead of defining them in a static class, you might as well declare the commands directly in XAML. Example (adapted from Guffas nice example):
与其在静态类中定义它们,不如直接在 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}" />
回答by Waleed A.K.
I Know that my answer is too late, but i hope it will help for the future.
我知道我的回答为时已晚,但我希望它对未来有所帮助。
I Like Guffa and Heinzi answers, but you can use only one command to achieve the previous result. I usually use the Help command
我喜欢 Guffa 和 Heinzi 的答案,但您只能使用一个命令来实现先前的结果。我通常使用帮助命令
<Window.CommandBindings>
<CommandBinding Command="{StaticResource Help}" Executed="HelpExecuted" />
</Window.CommandBindings>
and I use CommandParametr with each call e.g
我每次调用都使用 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>
or
或者
<Button Command="Help" CommandParameter="Case6" Content="Button">
<Button.InputBindings>
<KeyBinding Command="{StaticResource Help}" Gesture="Ctrl+D" CommandParameter="Case7"/>
</Button.InputBindings>
</Button>
and in the cs file
并在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;
}
and if you are using MVVM pattern
如果您使用的是 MVVM 模式
private void HelpExecuted(object sender, ExecutedRoutedEventArgs e)
{
string str = e.Parameter as string;
Mvvm_Variable.Action(Input: str);
e.Handled = true;
}
and move the switch to ViewModule site. and Action is a method in the same ViewModule class.
并将开关移动到 ViewModule 站点。和 Action 是同一个 ViewModule 类中的一个方法。

