弹出与StaysOpen = False

时间:2012-01-09 14:24:50

标签: wpf popup

我已经设置了一个带有三个矩形的简单控件,每个矩形都附有一个弹出窗口。最初所有三个弹出窗口都设置为打开(IsOpen = True),并且所有三个弹出窗口都将StaysOpen标志设置为false。这个XAML发布在下面。

从StaysOpen上的MSDN文档中,我收集到错误时,单击弹出窗口外的鼠标应关闭弹出窗口。我发现如果我完全在应用程序外单击鼠标,则所有三个弹出窗口都正确关闭。但是,如果我在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>

1 个答案:

答案 0 :(得分:2)

MSDN说“当StaysOpen为false时,Popup控件会拦截所有鼠标和键盘事件,以确定其中一个事件是否发生在Popup控件之外。”

我认为一次只能有一个弹出窗口(通过鼠标捕获),所以你的方法不起作用。我不知道在同一个父母身上打开多个弹出窗口通常是个好主意。

相关问题