wpf - 在 WPF 中使用多个修饰键创建 KeyBinding

标签 wpf

我创建KeyBinding的方式是这样的:

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

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

最佳答案

文档指出您可以使用 + 字符分隔修饰符:

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

参见here对于血淋淋的细节,下面提取了相关部分,以防链接消失:

<小时/>

XAML

<object property="oneOrMoreModifierKeys"/>

XAML 值

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

<小时/>

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

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

根据相同的文档链接:

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

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".

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

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 - 在 WPF 中使用多个修饰键创建 KeyBinding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4050066/

相关文章:

WPF ComboBox,强制输入到大写

c# - 如果文本框无效,则不禁用按钮

wpf - 用于在 MVVM 中进行配置的对话框窗口

wpf - 如何创建 WPF 圆角容器?

c# - 如何使用 Caliburn.Micro 制作导航服务?

c# - 'clr-namespace' URI 指的是未包含在程序集中的命名空间

wpf - 绑定(bind)到 DbSet<T>.Local 时如何处理 WPF 中的验证?

c# - 将 wpf 进度条与库中的函数同步的正确方法

c# - 可以在 WPF 的导航窗口中禁用 F5 键吗?

c# - 如何单击 RibbonMenuButton .Net