单击WPF弹出窗口时,不会导致应用程序聚焦 [英] WPF Popup not causing application to be focused when clicked on
问题描述
我有一个正在使用带有一些WPF控件的弹出窗口的控件,以及StaysOpen ="True".问题是当应用程序没有焦点时,单击弹出窗口时,应用程序没有得到焦点.我做了一些研究,看来这可能是由于弹出菜单旨在用于菜单,因此它们没有连接所有适当的Windows消息处理程序.这是演示问题的准系统样本:
I have a control that is using a popup with some WPF controls inside of it, and StaysOpen="True". The problem is when clicking on the popup when the application doesn't have focus, the application does not receive focus. I've done a bit of research, and it seems like this may be due to the fact that popups are meant to be used for menus, so they don't have all of the proper windows message handlers hooked up. Here is a barebones sample to demoing the problem:
<Window x:Class="TestWindowPopupBehavior.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:TestWindowPopupBehavior="clr-namespace:TestWindowPopupBehavior" Title="MainWindow" Height="350" Width="525">
<Grid>
<Popup StaysOpen="True" IsOpen="True" Placement="Center">
<ListBox>
<TextBlock>123</TextBlock>
<TextBlock>123</TextBlock>
<TextBlock>123</TextBlock>
<TextBlock>123</TextBlock>
<TextBlock>123</TextBlock>
<TextBlock>123</TextBlock>
</ListBox>
</Popup>
</Grid>
</Window>
- 运行应用程序.
- 与列表框进行交互,应该可以正常工作.
- 切换到另一个应用程序.
- 在应用程序未激活时单击列表框.什么都没发生
- 单击列表框之外的应用程序.
- 单击列表框.现在正在工作.
我期望在步骤4中发生的事情是应用程序将获得焦点,并且列表框将选择新项.
What I would expect to happen in step 4 would be the application would receive focus, and the listbox would select the new item.
有没有解决此问题的方法,或者我显然缺少什么?我正在用功能完善的窗口重写整个弹出代码,并重新实现我们拥有的行为,但这似乎很复杂,只是为了解决这样的小问题.
Are there any workarounds to this problem, or something obvious I'm missing? I'm looking at rewriting the whole popup code with full-fledged windows, and re-implementing the behavior we have, but that seems really complicated just to fix a small problem like this.
推荐答案
如果处理MouseLeftButtonDown
事件,则可以调用Window.Activate()
方法.但是您应该为每个元素-Popup
和所有TextBlock
编写它.
If you handle MouseLeftButtonDown
event, you can call Window.Activate()
method. but you should write it for each element - Popup
and for all TextBlock
s.
您可能遇到的问题是,在Windows上您可以交换鼠标按钮,从左变为右,反之亦然(但我不知道这是如何工作的),因此,可能是您必须处理MouseRightButtonDown
事件
The problem you can met with is that on Windows you can swap mouse buttons, where left became right and vice versa (but I don't know how this works), so, may be you have to handle MouseRightButtonDown
event.
这篇关于单击WPF弹出窗口时,不会导致应用程序聚焦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!