我的ContextMenu不会开

我的ContextMenu不会开

本文介绍了WPF:当Alt键是pressed,我的ContextMenu不会开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WPF应用程序,当文本菜单正在打开,我要调整其菜单项取决于是否替代关键是pressed。

In my WPF app, when a ContextMenu is opening, I want to tweak its menu items depending on whether or not the Alt key is pressed.

我已经得到了逻辑工作。 XAML:

I've got the logic working. XAML:

<my:Control ContextMenuOpening="MyContextMenu_Opening" />

code:

Code:

private void MyContextMenu_Opening(object sender, RoutedEventArgs args) {
  bool isAltDown = Keyboard.IsKeyDown(Key.LeftAlt);
  /* tweak menu items here */
}

我的问题是,当替代关键是pressed,上下文菜单打开,然后立即关闭(我可以在里面被打开了的闪光见我的逻辑是至少工作)。

My problem is that when the Alt key is pressed, the context menu opens and then immediately closes (I can see in the flash of it being open that my logic is at least working).

我不知道这是一个WPF一般'功能',因为如果我甚至持有替代同时右击一个文本字段,同样的事情发生 - 内置剪切/复制/粘贴菜单闪烁打开,然后立即关闭

I wonder is this a WPF 'feature' in general because if I even hold Alt while right clicking on a TextField, the same thing happens - the built-in Cut/Copy/Paste menu flashes open then immediately closes.

一个直觉是,它是与替代激活应用程序菜单栏。但是,一个应用程序菜单栏并不适用于我的情况,因此,如果解决方案涉及与搞乱,它仍然会为我工作。

One hunch is that it has something to do with Alt activating the application menu bar. But an application menu bar does not apply to my situation, so if the solution involves messing with that, it will still work for me.

推荐答案

这是在MenuBase类内置的行为:

This is a built-In behavior in the MenuBase class:

        protected override void OnKeyDown(KeyEventArgs e)
        {
            .....
            if (((e.SystemKey == Key.LeftAlt) || (e.SystemKey == Key.RightAlt)) || (e.SystemKey == Key.F10))
            {
                this.KeyboardLeaveMenuMode();
                e.Handled = true;
            }
        }

为什么不使用另一个键修改??

Why not use another Modifier Key ??

这篇关于WPF:当Alt键是pressed,我的ContextMenu不会开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 18:44