In wpf with vb, there is no closing of a popup opened from another popup with the Staysopen property set to true.

jacky Perpète 216 Reputation points
2026-08-08T17:23:32.26+00:00

Hello,

In WPF with VB, I have a main window representing a toolbar.

A button in the main window opens a popup with the StaysOpen property set to True.

From a button in the popup, I open a second popup with StaysOpen set to False.

After the first time the second popup opens, it closes correctly.

After subsequent openings, this popup no longer closes when clicked outside its designated area.

Using AI, I've tried many solutions without success.

Does anyone have a solution for this problem?

Attached is a zipped test program (PopupTest.txt).

Please replace the .txt extension with .zip.

PopupTest.txt

Popup

<Window x:Class="MainWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
         KeyboardNavigation.TabNavigation="None"
         SizeToContent="WidthAndHeight"
         Title="MainWindow"  WindowStyle="None" ResizeMode="NoResize" ShowInTaskbar="False" WindowStartupLocation="CenterScreen" AllowsTransparency="True" Background="Transparent" Topmost="True">
    <Grid>
        <Border Background="#FF3B333B" CornerRadius="10" BorderBrush="#FF989898" BorderThickness="1" Height="40">
            <StackPanel Orientation="Horizontal">
                <Button x:Name="btnOpenPopup1" Height="32" Width="100" Content="Open Popup 1"/>
                <Border Width="1" Height="32" Background="White" VerticalAlignment="Center" Margin="2,0" />
                <Button x:Name="btnClosePopup" Height="32" Width="100" Margin="2,0" Content="Close Popup"/>
                <Border Width="1" Height="32" Background="White" VerticalAlignment="Center" Margin="2,0" />
                <Button x:Name="btnClose" Height="32" Width="100" Margin="2,0" Content="Close X"/>
            </StackPanel>
        </Border>

        <Popup x:Name="Popup1" IsOpen="False" Placement="Bottom" AllowsTransparency="True" VerticalOffset="10" StaysOpen="True">
            <Border Background="#FF3B333B" CornerRadius="10" BorderBrush="#FF989898" BorderThickness="1" Height="40">
                <StackPanel Orientation="Horizontal">
                    <Label Content="Popup 1 - StaysOpen=True" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    <Button x:Name="btnOpenPopup2" Height="32" Width="100" Content="Open Popup 2"/>   
                </StackPanel>
            </Border>
        </Popup>
        
        <Popup x:Name="Popup2" IsOpen="False" Placement="Bottom" StaysOpen="False">
            <Border BorderThickness="1" BorderBrush="#FF909090" Background="#FF4B434B">
                <Grid>
                    <Label Content="Popup 2 - StaysOpen=False" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Grid>
            </Border>
        </Popup>

    </Grid>
</Window>


Class MainWindow
    Private Sub btnOpenPopup1_Click(sender As Object, e As RoutedEventArgs) Handles btnOpenPopup1.Click

        'Affiche le popup 1
        Popup1.PlacementTarget = btnOpenPopup1
        Popup1.IsOpen = True
    End Sub

    Private Sub btnOpenPopup2_Click(sender As Object, e As RoutedEventArgs) Handles btnOpenPopup2.Click

        'Affiche le popup 2
        Popup2.PlacementTarget = btnOpenPopup2
        Popup2.IsOpen = True
    End Sub

    Private Sub btnClose_Click(sender As Object, e As RoutedEventArgs) Handles btnClose.Click

        'Ferme la fenêtre principale
        Close()
    End Sub

    Private Sub btnClosePopup_Click(sender As Object, e As RoutedEventArgs) Handles btnClosePopup.Click

        'Ferme les popups
        Popup1.IsOpen = False
        Popup2.IsOpen = False
    End Sub

End Class

Developer technologies | VB

Answer accepted by question author
Danny Nguyen (WICLOUD CORPORATION) 8,535 Reputation points Microsoft External Staff Moderator
2026-08-10T03:22:52.56+00:00

Hi @jacky Perpète

Thank you for reaching out and confirming that gekka's solution worked for you!

Just to add a bit of context on why this fix should work:

Popups in WPF are not just ordinary UI framework elements; they create their own top-level Win32 windows (using HwndSource) that float above the application. When you have a cascading popup scenario (Main Window -> Popup1 -> Popup2), the window focus and activation state get complex.

When StaysOpen="False", the Popup relies on mouse capture to detect when a user clicks outside of its bounds to close itself.

  • The first time you open Popup2, the main application window is fully active, so Popup2 captures the mouse correctly and closes as expected.
  • However, when Popup2 closes and releases mouse capture, the OS window activation state can shift such that the main window is no longer considered the strictly "Active" foreground window.
  • On subsequent openings, because the main window lacks proper OS-level activation, Popup2's attempt to capture the mouse silently fails. Without mouse capture, it cannot detect external clicks and stays open.

By explicitly calling Me.Activate() (or activating the owner window), you force WPF and the OS to bring the main Window to the forefront. This restores the active window state, allowing the popup to successfully capture the mouse and detect outside clicks once again.

Popup Overview (Discusses popup visual tree and window relationships)

If you found my response helpful or informative, I would greatly appreciate it if you could follow this guidance or provide feedback.

Thank you.

Was this answer helpful?

1 person found this answer helpful.

Answer accepted by question author
gekka 14,226 Reputation points MVP Volunteer Moderator
2026-08-09T03:57:06.0333333+00:00

The issue where cascading popups fail to close as expected can be resolved by activating the owner window.

Private Sub btnOpenPopup2_Click(sender As Object, e As RoutedEventArgs) Handles btnOpenPopup2.Click

    'Affiche le popup 2
    Popup2.PlacementTarget = btnOpenPopup2
    Popup2.IsOpen = True

    Dim w = Window.GetWindow(Popup2.PlacementTarget)
    If w Is Nothing Then
        Application.Current.MainWindow.Activate()
    Else
        w.Activate()
    End If
End Sub

Was this answer helpful?

1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. jacky Perpète 216 Reputation points
    2026-08-09T16:09:26.7966667+00:00

    The solution proposed by gekka solves my problem.

    I just simplified it.

    Private Sub btnOpenPopup2_Click(sender As Object, e As RoutedEventArgs) Handles btnOpenPopup2.Click
    
        'Affiche le popup 2
        Popup2.PlacementTarget = btnOpenPopup2
        Popup2.IsOpen = True
    
        Me.Activate()
    
    End Sub
    
    

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.