为什么设置 MenuItem.InputGestureText 不会导致当我执行输入手势时激活 MenuItem?
我想为 MenuItem
实现键盘快捷键。我使用了下面的代码:
<MenuItem Header="_New" InputGestureText="CTRL+N" Click="NewMenu_Click">
<MenuItem.Icon>
<Image Source= "Images\NEW.PNG" Width="25" Height="28" />
</MenuItem.Icon>
</MenuItem>`
但是当我按下 CTRL+N
时,InputGestureText
属性没有响应。我正在使用 Visual Studio Express Edition 2010。我在这里遗漏了什么吗?
I want to implement Keyboard shortcuts for a MenuItem
. I have used the code below:
<MenuItem Header="_New" InputGestureText="CTRL+N" Click="NewMenu_Click">
<MenuItem.Icon>
<Image Source= "Images\NEW.PNG" Width="25" Height="28" />
</MenuItem.Icon>
</MenuItem>`
But the InputGestureText
property is not responding when I pressed the CTRL+N
. I am using Visual Studio Express Edition 2010. Am I missing anything here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该属性的文档中非常明确:
It is quite explicit in the documentation for the property:
执行此操作的最佳方法是创建一个
Command
,并将InputGesture
与该命令相关联:如果您确实只想要一个“新建”命令,则可以跳过创建
RoatedCommand
和InputGesture
,因为该命令已为您创建:The best way to do this is to make a
Command
, and associate theInputGesture
with that command:If you really just want a "New" command, you can skip creating the
RoutedCommand
andInputGesture
, because that command is already created for you:一种不涉及命令和绑定的解决方案是重写所属窗口的
OnKeyDown
方法,并搜索具有与键盘事件匹配的KeyGesture
的菜单项。下面是 Window 的 OnKeyDown 覆盖的代码:
下面是将菜单项与键盘事件相匹配的实用程序代码:
One solution that doesn't involve commands and bindings is to override the owning Window's
OnKeyDown
method and search a menu item that has aKeyGesture
that matches the keyboard event.Here is the code for the Window's OnKeyDown override:
And here is the utility code that matches a menu item with the keyboard event: