弹出窗口 StaysOpen = False

发布于 2024-12-25 11:31:18 字数 2124 浏览 1 评论 0原文

我设置了一个带有三个矩形的简单控件,每个矩形都附加了一个弹出窗口。最初,所有三个弹出窗口都设置为打开 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

用心笑 2025-01-01 11:31:18

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文