在 WPF 中使用多个修饰键创建 KeyBinding [英] Creating KeyBinding in WPF with more than one modifier key

查看:54
本文介绍了在 WPF 中使用多个修饰键创建 KeyBinding的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建 KeyBinding 的方式类似于:

The way I created KeyBinding was something like:

<KeyBinding Modifiers="Ctrl" Key="S" Command="{Binding SaveCommand}" />

但是如果我需要两个修饰键怎么办?例如,Ctrl + Shift.

But what if I needed two modifier keys? For example, Ctrl + Shift.

推荐答案

文档说明你可以用 + 字符分隔修饰符:

The documentation states that you can just separate the modifiers with the + character:

<KeyBinding Modifiers="Ctrl+Shift" Key="S" Command="{Binding SaveCommand}" />

请参阅此处了解详细信息,在下面提取相关位,以防链接消失:

See here for the gory details, with the relevant bits extracted below in case the link ever disappears:

XAML

<object property="oneOrMoreModifierKeys"/>

XAML 值

oneOrMoreModifierKeys — 一个或多个修饰键,由 ModifierKeys 枚举定义,用 + 字符分隔.

oneOrMoreModifierKeys — One or more modifier keys, defined by the ModifierKeys enumeration, delimited with a + character.

您也可以单独使用手势而不是键/修饰符组合:

You can also use a gesture on its own rather than a key/modifier combo:

<KeyBinding Gesture="Ctrl+Shift+S" Command="{Binding SaveCommand}" />

根据相同的文档链接:

在 XAML 中定义 KeyBinding 时,有两种方法可以指定 KeyGesture.

When defining a KeyBinding in XAML, there are two ways to specify the KeyGesture.

在 XAML 中建立 KeyBinding 的第一种方法是定义 KeyBinding 元素的 Gesture 属性,这使语法能够将键和修饰符指定为单个字符串,例如CTRL+P".

The first way to establish a KeyBinding in XAML is to define the Gesture attribute of the KeyBinding element, which enables a syntax to specify keys and modifiers as a single string, for example "CTRL+P".

第二种方式是定义 KeyBinding 元素的 Key 属性和 Modifiers 属性.

The second way is to define the Key attribute and the Modifiers attributes of the KeyBinding element.

这两种设置KeyGesture的方式是等价的,修改的都是同一个底层对象,但是如果同时使用就会有冲突.Key、Modifiers、Gesture属性都设置的情况下,KeyGesture使用最后定义的属性.

Both ways of setting the KeyGesture are equivalent and modify the same underlying object, but there will be a conflict if both are used. In the case when the Key, Modifiers, and the Gesture attributes are all set, the attribute which is defined last will be used for the KeyGesture.

这篇关于在 WPF 中使用多个修饰键创建 KeyBinding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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