简体   繁体   English

按住修饰键时,所有 WPF 上下文菜单似乎都无法正确处理突出显示的项目

[英]All WPF context menus do not seem to handle highlighting items properly when a modifier key is held down

It happens in all my WPF applications and Visual Studio 2019 (which is definitely WPF based) also exhibits this strange behavior:它发生在我所有的 WPF 应用程序中,并且 Visual Studio 2019(绝对基于 WPF)也表现出这种奇怪的行为:

Simply right click an item in the solution explorer while holding, say, the Control key and you should notice that the items highlighting will intermittently work if you keep holding the Control modifier.只需在按住 Control 键的同时右键单击解决方案资源管理器中的一个项目,您应该注意到,如果您继续按住 Control 修饰符,突出显示的项目会间歇性地工作。

At first I presumed that the grids and lists controls were still catching the modifier keys for the items selection but this issue also occurs with a context menu on a simple control like a standard button.起初,我认为网格和列表控件仍在捕获用于项目选择的修饰键,但是这个问题也会出现在像标准按钮这样的简单控件上的上下文菜单中。

Is there a way to fix this glitch?有没有办法修复这个故障?


Here is a gif with wpf application context menu in action.这是一个带有 wpf 应用程序上下文菜单的 gif。 First I move mouse normally, then with Ctrl hold down:首先我正常移动鼠标,然后按住 Ctrl 键

As you can see it glitch (is not highlighting menu items).如您所见,它出现故障(未突出显示菜单项)。

MenuItem sets an internal flag when a key is pressed that stops mouse events from being registered temporarily, regardless of if it is actually a key that performs navigation. MenuItem 会在按下某个键时设置一个内部标志,以暂时停止注册鼠标事件,而不管它是否实际上是一个执行导航的键。 This reflects the behavior that I see, which is that the selection stutters when I hold down any key, not only modifiers.这反映了我所看到的行为,即当我按住任何键时,选择会断断续续,而不仅仅是修饰符。 https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/windows/Controls/MenuItem.cs,2049 https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/windows/Controls/MenuItem.cs,2049

Since it's private there is no proper way to fix this.由于它是私有的,因此没有正确的方法来解决这个问题。 However if it is critical you can add a KeyDown handler for all your MenuItem s and then change it with reflection但是,如果它很重要,您可以为所有MenuItem添加 KeyDown 处理程序,然后使用反射更改它

var menu = sender as MenuItem;
if (menu != null)
{
    var parent = ItemsControl.ItemsControlFromItemContainer(menu);
    MethodInfo setBoolField = menu.GetType().GetMethod("SetBoolField",
        BindingFlags.NonPublic | BindingFlags.Static);
    setBoolField.Invoke(this, new object[] { parent, 0x04, false });
}

If you want you can first check if the key was a navigation key to retain the desired behavior.如果需要,您可以先检查该键是否为导航键以保留所需的行为。

I would personally consider this a bug in WPF.我个人认为这是 WPF 中的一个错误。

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

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