创建一个简单的,未经修改的重点在WPF绑定 [英] Create a simple, unmodified key binding in WPF

查看:224
本文介绍了创建一个简单的,未经修改的重点在WPF绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个非常简单的WPF应用程序。我想有一个叫做命令处理程序,当用户pressesH(不控制-H或者Alt-H,只是H)。如果我使用code-背后code以下内容:

I'm trying to create a very simple WPF application. I'd like to have an command handler called when the user presses "H" (not Control-H or Alt-H, just H). If I use the following in code-behind code:

var gesture = new KeyGesture(Key.H, ModifierKeys.None);
var inputBinding = new InputBinding(MyRoutedCommand, gesture);
InputBindings.Add(inputBinding);

我立即得到一个异常说,关键和修饰符的组合不支持KeyGesture。

I immediately get an exception saying that the key and modifier combination isn't supported for KeyGesture.

(我得到同样的异常,如果我创建XAML做类似的事情)。

(I get the same exception if I create the XAML to do the analogous thing).

我的应用程序不接受输入,没有文本框是在应用程序中,所以没有真正的文字(至少在我的脑海里)发生冲突。我也不会想到,这将是如此不同寻常。

My application doesn't accept typed input, no text boxes are in the application, so there isn't really a conflict with text (at least in my mind). I wouldn't have thought that this would be so unusual.

我见过其他地方的意见是认为我可以在低级别的这个或截获键盘presses创建一个自定义的控制,但这些似乎是一个很大的开销。任何简单的想法?

I've seen comments elsewhere that suggest that I could create a custom control for this or intercept keyboard presses at a low level, but these seem like a lot of overhead. Any simpler thoughts?

推荐答案

不要使用 KeyGesture 类型或属性(如一个关键的不是姿态)。

Don't use the KeyGesture type or property (as one key is not a gesture).

例如。

KeyBinding b = new KeyBinding()
    {
        Command = MyRoutedCommand,
        Key = Key.H
    };
InputBindings.Add(b);

相同的XAML

Same for XAML

<KeyBinding Command="{Binding MyRoutedCommand}" Key="H" />

这篇关于创建一个简单的,未经修改的重点在WPF绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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