如何在复合 WPF 中将按键与 DelegateCommand 相关联?
我正在使用 CAL/Prism 构建一个复合应用程序。主要区域是一个选项卡控件,其中有多种类型的视图。每个视图都有一个可以处理的自定义设置命令,这些命令绑定到窗口顶部的工具栏按钮。我之前在非 CAL 应用程序中通过简单地在命令上设置 InputBinding 来完成此操作,但我无法在 CAL 模块的源代码中找到任何此类机制。
我的问题是,将击键连接到我的视图的最佳方法是什么,以便当用户按下 Alt + T 时,关联的 DelegateCommand 对象会处理它?连接捷径并不是那么困难......
I am building a composite application using CAL/Prism. The main region is a tab control, with multiple types of views in it. Each view has a custom set commands that it can handle which are bound to toolbar buttons at the top of the window. I've done this before in non-CAL apps by simply setting the InputBinding on the command, but I haven't been able to find any such mechanism in the source code for the CAL modules.
My question is, what is the best way to hook up a keystroke to my view, so that when the user presses Alt + T, the associated DelegateCommand object handles it? Hooking up a shortcut can't be THAT difficult...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅供参考,CommandReference 类当前未包含在您可以引用的程序集中,但包含在 MV-VM 项目模板中。因此,如果您不从模板构建应用程序,那么您必须从其他地方获取该类。我选择从示例项目中复制它。我将其包含在下面是为了让每个人都能轻松访问这一小块优点,但请务必在 MV-VM Toolkit 的未来版本中检查模板的更新。
享受!
Just for reference, the CommandReference class is currently not included in an assembly that you can reference, but is included with the M-V-VM project template. So if you don't build your application from the template, then you have to get the class from somewhere else. I chose to copy it from the sample project. I included it below to allow everyone easy access to this little chunk of goodness, but be sure to check for updates to the template in future versions of the M-V-VM Toolkit.
Enjoy!
MVVM 工具包 有一个类称为
CommandReference
,它允许您使用对命令的引用作为键绑定。这样就可以了。
编辑:自本文编写以来,WPF 4.0 修复了此特定问题,您不再需要使用静态资源解决方法。您可以直接从 KeyBinding 引用视图模型中的命令。
The MVVM Toolkit has a class called a
CommandReference
that will allow you to use a reference to a command as a keybinding.This'll do it.
Edit: Since this was written, WPF 4.0 fixed this particular issue and you no longer have to use the static resource workaround. You can reference the command in your viewmodel directly from the KeyBinding.