wpf 在我单击其中的控件之前,弹出窗口不会失去焦点并关闭
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16316139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Popup doesn't lose focus and close until I've clicked a control within it
提问by Anders Madsen
I'm trying to create a dropdown control consisting of a ToggleButton and a Popup control with a TabControl inside. My problem is, that the Popup doesn't close automatically unless I've clicked a certain control inside it.
我正在尝试创建一个下拉控件,它由一个 ToggleButton 和一个带有 TabControl 的 Popup 控件组成。我的问题是,除非我点击了其中的某个控件,否则 Popup 不会自动关闭。
Consider the example below where the popup contains a TabControl which itself contains a Calendar control inside a TabItem.
考虑下面的示例,其中弹出窗口包含一个 TabControl,它本身在 TabItem 中包含一个 Calendar 控件。
The expected behavior is that the Popup Closes whenever it loses focus (i.e. clicking the container window), but in order for the popup to fire a LostFocus event and thus closing, I have to click one of the arrow buttons on the Calendar control first.
预期的行为是弹出窗口在失去焦点时关闭(即单击容器窗口),但为了让弹出窗口触发 LostFocus 事件并因此关闭,我必须先单击日历控件上的箭头按钮之一。
<UserControl
x:Class="DropDownExample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
<Grid>
<ToggleButton x:Name="ToggleButton"
ClickMode="Press">Example</ToggleButton>
<Popup x:Name="Popup"
Placement="Bottom"
AllowsTransparency="True"
StaysOpen="False"
PopupAnimation="Slide"
FocusManager.IsFocusScope="false">
<TabControl x:Name="TabControl"
MinHeight="200">
<TabItem>
<Calendar />
</TabItem>
</TabControl>
</Popup>
</Grid>
</UserControl>
The opening/closing of the Popup is controlled in the Checked/Unchecked events of the ToggleButton.
Popup 的打开/关闭由 ToggleButton 的 Checked/Unchecked 事件控制。
回答by alexv
The problem is in that ClickMode=Press. Setting ClickMode=Release fixes the problem and Popup closes on focus lost.
问题在于 ClickMode=Press。设置 ClickMode=Release 解决了这个问题,Popup 在失去焦点时关闭。
回答by feO2x
I have no problem closing the Popup when I click anywhere else on the screen using this code:
当我使用以下代码单击屏幕上的任何其他地方时,关闭弹出窗口没有问题:
<Window x:Class="AutomaticPopupClosing.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="100" Width="240">
<Grid>
<Button Content="Show Popup"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Click="ButtonBase_OnClick" />
<Popup x:Name="Popup"
StaysOpen="False"
FocusManager.IsFocusScope="False"
PopupAnimation="Slide"
AllowsTransparency="True">
<Border Padding="5"
Background="White"
FocusManager.IsFocusScope="False">
<StackPanel>
<TabControl x:Name="TabControl" MinHeight="200">
<TabItem>
<Calendar />
</TabItem>
</TabControl>
</StackPanel>
</Border>
</Popup>
</Grid>
</Window>
In the ButtonBase_OnClickmethod I just assign trueto the Popup.IsOpenproperty:
在ButtonBase_OnClick我刚刚分配true给Popup.IsOpen属性的方法中:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
Popup.IsOpen = true;
}
Do you have anything else that is worth noting? I could not reconstruct your problem.
你还有什么值得注意的吗?我无法重建你的问题。
Edit: after reading your comments, I tried to move the above code in a user control. The code is basically the same:
编辑:阅读您的评论后,我尝试在用户控件中移动上述代码。代码基本相同:
<UserControl x:Class="PopupDoesNotClose.PopupCalendar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="100">
<Grid>
<Button Content="Show Popup"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Click="ButtonBase_OnClick" />
<Popup x:Name="Popup"
StaysOpen="False"
FocusManager.IsFocusScope="False"
PopupAnimation="Slide"
AllowsTransparency="True">
<Border Padding="5"
Background="White">
<StackPanel>
<TabControl x:Name="TabControl"
MinHeight="200">
<TabItem>
<Calendar />
</TabItem>
</TabControl>
</StackPanel>
</Border>
</Popup>
</Grid>
</UserControl>
MainWindownow looks like this:
MainWindow现在看起来像这样:
<Window x:Class="PopupDoesNotClose.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:PopupDoesNotClose"
Title="MainWindow"
Height="100"
Width="240">
<local:PopupCalendar />
</Window>
When I open the popup and try to move the window afterwards using its title bar, the popup will close and I have to click and drag the title bar again to actually perform the move operation. Is there still something in your code that is not part of your question? I still cannot reconstruct your problem.
当我打开弹出窗口并随后尝试使用其标题栏移动窗口时,弹出窗口将关闭,我必须再次单击并拖动标题栏才能实际执行移动操作。您的代码中是否还有某些内容不属于您的问题?我仍然无法重建您的问题。

