首页 > 解决方案 > 如何从后面的代码在 XAML 中添加手势

问题描述

所以我有这个KeyBinding

<Window.InputBindings>
    <KeyBinding Gesture="Ctrl+Alt+S" Command="{Binding TestCommand}"
                HotkeyManager.RegisterGlobalHotkey="True" />
</Window.InputBindings>

而且我的应用程序有几个ToggeButtons用户需要指定快捷方式,并且我希望能够从后面的代码中更改它:

Gesture="Ctrl+Alt+S"

有什么建议么 ?

标签: wpfxamlkey-bindings

解决方案


创建一个KeyGesture. 这ModifierKeys是一个标志enum

KeyBinding binding = new KeyBinding()
{ 
    Gesture = new KeyGesture(Key.S, ModifierKeys.Control | ModifierKeys.Alt) 
};

推荐阅读