繁体   English   中英

WPF中的LeftAlt键绑定

[英]LeftAlt Keybinding in WPF

我试图用命令绑定左ALT键以切换WPF中菜单的可见性。 但这不起作用..命令没有触发..

<Window.InputBindings>
        <KeyBinding
            Key="LeftAlt"
            Command="{Binding Path=MenuVisibilitySetCommand}"/>
</Window.InputBindings>

我注意到其他特殊键(例如Alt,Ctrl等)在这里也无法使用。

如何在WPF中对特殊键进行键绑定?

为了使LeftALt像这样工作,您还需要将Modifiers属性设置为Alt

<KeyBinding Key="LeftAlt" Modifiers="Alt" Command="{Binding Path=MenuVisibilitySetCommand}"/>

这些特殊的键称为“修饰键”,这应该清楚说明为什么它不起作用。 修饰键用于“修饰”给定键的行为,例如Shift + L表示大写字母“ L”,只有L按键表示小写字母“ l”。 使用Modifierkeys进行实际逻辑操作可能会引起麻烦和烦人,因为用户不习惯于按这些按钮时看到发生的实际动作。 但是我同意有些地方是有意义的,例如在按ALT键时突出显示MenuItems。

但要解决您的实际问题:您可以使用codebehind和OnKeyDown / OnKeyUp或Preview事件来实现此行为。

    protected override void OnKeyDown(KeyEventArgs e)
    {
        if(e.SystemKey == Key.LeftAlt)
        {
            myMenu.Visibility = Visibility.Visible;
            // e.Handled = true; You need to evaluate if you really want to mark this key as handled!
        }

        base.OnKeyDown(e);
    }

当然,cou也可以在此代码中触发您的命令。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM