Stephen Holt
Stephen Holt

Reputation: 2368

Popup with StaysOpen = False

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>

Upvotes: 2

Views: 5110

Answers (1)

Clemens
Clemens

Reputation: 128061

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.

Upvotes: 2

Related Questions