WPF中的ProcessCmdKey模拟 [英] ProcessCmdKey analog in WPF

查看:249
本文介绍了WPF中的ProcessCmdKey模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是WPF和WinForms的新手,可以将热键添加到我通常使用的表单中 ProcessCmdKey 使其易于覆盖(添加)与密钥相关的功能(类似于这里).有没有一种简单的方法可以在WPF中分配热键?我正在使用带有键的命令,但有时不起作用(我认为窗口上的其他控件会响应该手势并执行其工作,以使我的命令无法响应预定义的键手势).

I'm new to WPF and in WinForms to add hotkeys to the form I've usually used ProcessCmdKey that made easy to override(add) key related functionality (similar to the way described here). Is there an easy way to assign hotkeys in WPF? I'm using Commands with keys, but sometimes that doesn't work (I think some other controls on window respond to that gestures and do their jobs, so that my command can't respond to predefined key gesture).

推荐答案

InputBindings的作用域是它们所分配的控件,如果在Window.InputBindings中分配KeyBinding,则它们将在整个窗口中触发,除非通过在树下的某个位置定义相同的手势,可以局部覆盖该手势.

InputBindings are scoped with the control they are assigned to, if you assign a KeyBinding in the Window.InputBindings they will be fired throughout the window unless the same gesture is locally overridden by being defined somewhere down the tree.

例如:

<UserControl.InputBindings>
    <KeyBinding Gesture="CTRL+SHIFT+N"
                Command="{Binding BtnNewChild_Command}"
                CommandParameter="{Binding ElementName=view}" />
</UserControl.InputBindings>

不起作用,它不能将view元素作为参数传递,除非该UserControl中有一个名为"view"的控件. wpf是否有一些通用"热键分配方案?

doesnt work, it cant pass the view element as parameter, tho there is control named "view" in that UserControl. Does wpf have some "common" hotkey assign scenario?

这里的问题只是范围,子控件可以访问在树上更高的位置声明的内容,但不能访问其他内容.您可以重构此视图以在UserControl.Resources中将其创建为资源,然后可以在CommandParameter中引用它,也可以在UserControl中使用的任何地方引用它.

The problem here would just be scope, the child controls have access to things declared higher up on the tree but not the other way around. You could possibly refactor this to create your view as a resource in the UserControl.Resources then you can reference it both in the CommandParameter and whereever you use it in the UserControl.

这篇关于WPF中的ProcessCmdKey模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆