栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

我使用放置在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));}

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

xmlns:w="clr-namespace:NameSpaceOfTheApplication"

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

<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" />


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/432929.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号