弹出窗口 StaysOpen = False
我设置了一个带有三个矩形的简单控件,每个矩形都附加了一个弹出窗口。最初,所有三个弹出窗口都设置为打开 (IsOpen=True),并且所有三个弹出窗口的 StaysOpen 标志都设置为 false。下面发布了相关的 XAML。
从 StaysOpen 的 MSDN 文档中,我了解到,当它为 false 时,单击弹出窗口外部的鼠标应该关闭弹出窗口。我发现,如果我完全在应用程序外部单击鼠标,则所有三个弹出窗口都会正确关闭。但是,如果我在 WPF 窗口本身内单击,则只有顶部弹出窗口会关闭。另外两个仍然可见。
有谁知道这里发生了什么,以及我能做些什么来确保所有三个弹出窗口按预期关闭?
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Rectangle x:Name="Rect1" Fill="DarkBlue"/>
<Rectangle x:Name="Rect2" Fill="Orange" Grid.Row="2"/>
<Rectangle x:Name="Rect3" Fill="DarkRed" Grid.Row="4"/>
<Popup PlacementTarget="{Binding ElementName=Rect1}" Placement="Right" IsOpen="True" StaysOpen="False">
<Border BorderBrush="Black" BorderThickness="2" Background="Wheat" Width="200" Height="100">
<TextBlock>Popup 1</TextBlock>
</Border>
</Popup>
<Popup PlacementTarget="{Binding ElementName=Rect2}" Placement="Right" IsOpen="True" StaysOpen="False">
<Border BorderBrush="Black" BorderThickness="2" Background="Wheat" Width="200" Height="100">
<TextBlock>Popup 2</TextBlock>
</Border>
</Popup>
<Popup PlacementTarget="{Binding ElementName=Rect3}" Placement="Right" IsOpen="True" StaysOpen="False">
<Border BorderBrush="Black" BorderThickness="2" Background="Wheat" Width="200" Height="100">
<TextBlock>Popup 3</TextBlock>
</Border>
</Popup>
</Grid>
</Window>
I have set up a simple control with three rectangles, each of which has a popup attached to it. Initially all three popups are set to open (IsOpen=True), and all three have the StaysOpen flag set to false. The XAML for this is posted below.
From the MSDN documentation on StaysOpen, I gather that when it is false, clicking the mouse outside of the popup should close the popup. What I am finding is that if I click the mouse completely outside the application, then all three popups close correctly. However, if I click within the WPF window itself then only the top popup closes. The other two remain visible.
Does anybody know what is happening here, and what I can do to ensure that all three popups close as expected?
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Rectangle x:Name="Rect1" Fill="DarkBlue"/>
<Rectangle x:Name="Rect2" Fill="Orange" Grid.Row="2"/>
<Rectangle x:Name="Rect3" Fill="DarkRed" Grid.Row="4"/>
<Popup PlacementTarget="{Binding ElementName=Rect1}" Placement="Right" IsOpen="True" StaysOpen="False">
<Border BorderBrush="Black" BorderThickness="2" Background="Wheat" Width="200" Height="100">
<TextBlock>Popup 1</TextBlock>
</Border>
</Popup>
<Popup PlacementTarget="{Binding ElementName=Rect2}" Placement="Right" IsOpen="True" StaysOpen="False">
<Border BorderBrush="Black" BorderThickness="2" Background="Wheat" Width="200" Height="100">
<TextBlock>Popup 2</TextBlock>
</Border>
</Popup>
<Popup PlacementTarget="{Binding ElementName=Rect3}" Placement="Right" IsOpen="True" StaysOpen="False">
<Border BorderBrush="Black" BorderThickness="2" Background="Wheat" Width="200" Height="100">
<TextBlock>Popup 3</TextBlock>
</Border>
</Popup>
</Grid>
</Window>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MSDN 说“当 StaysOpen 为 false 时,Popup 控件会拦截所有鼠标和键盘事件,以确定这些事件之一何时发生在 Popup 控件之外。”
我认为一次只有一个弹出窗口可以执行此操作(通过鼠标捕获),因此您的方法不起作用。我不知道在同一个父级上打开多个打开的弹出窗口通常是否是一个好主意。
The MSDN says "When StaysOpen is false, the Popup control intercepts all mouse and keyboard events to determine when one of these events occurs outside the Popup control."
I think only one popup can do this (by mouse capture) at a time, so your approach won't work. I don't know if it is generally a good idea to have more than one open popup on the same parent.