问题描述
我有一个包含菜单和子菜单的应用程序。我重视Appliocation命令的一些子菜单项,如剪切,复制和粘贴的。
我也有不具有应用程序命令一些其他菜单项。
我怎么可以添加自定义命令绑定到这些子菜单项?
我已经通过文章,但无法附加事件到我的子菜单项。
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 this article but unable to attach event to my sub menu items.
推荐答案
我用我的窗口1类(或任何窗口类碰巧被命名),其中我创建RoutedUICommand类的实例后放置一个静态类:
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:
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));
}
在窗口标记添加一个命名空间,使用命名空间的窗口1类是:
Add a namespace in the window markup, using the namespace that the Window1 class is in:
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" />
这篇关于如何添加一个自定义WPF路由命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!