如何向菜单项添加访问键?
<MenuItem x:Name="newProjectButton" Click="OnNewProjectButton_Click" Header="_New Project">
</MenuItem>
我想在按下 Alt+N 时调用 OnNewProjectButton_Click 。不幸的是,上面的代码不起作用,因为只有在菜单展开(即具有焦点)时才会调用处理程序。
<MenuItem x:Name="newProjectButton" Click="OnNewProjectButton_Click" Header="_New Project">
</MenuItem>
I want to call OnNewProjectButton_Click whenever Alt+N is pressed. The code above doesn't work, unfortunately, because the handler is called only if the menu is expanded (i.e. has focus).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 ApplicationCommands.New为此,因为它已经提供了该功能。默认的 WPF 命令模型非常酷。即使您决定不使用默认的命令模型,第二个链接也应该向您展示如何连接所需的输入手势。
编辑:这是一个示例实现...
以及背后的代码...
You could use ApplicationCommands.New for this since it already provides that functionality. The default WPF Command Model is pretty cool. Even if you decide not to use the default commanding model, that second link should show you how to hook up the input gestures you need.
EDIT: Here is a sample implementation...
And the code behind...
您会看到设置 InputGestureText 但与WinForms
不同的是,"应用程序必须处理用户的输入执行该操作”。
因此,请考虑使用 WPF 命令,因为它们会自动为您执行此操作。我发现 Windows Presentation Foundation Unleashed 很好地涵盖了这一点。
You see to set the InputGestureText on the menu item
But unlike WinForms the "The application must handle the user's input to carry out the action".
So consider using the WPF commands as they do this for you automatically. I found that Windows Presentation Foundation Unleashed covers this well.