WPF Close Popup on Button click in VB.NET

In this article we demonstrate on how to close a Popup on Button click.which work as a Container.
  • 5377

Here in this article we are discussing that how can you close a Popup on Button click in WPF. A Popup is a new primitive control in the WPF framework. A Popup is considered as a container that appears suddenly when some action is taken. A Popup window floats over a page or window providing functionality for some quick action. Popups are represented by the Popup class. The implementation of example needs StackPanel, DockPanel, Popup and Button control.

Getting Started

  • Simply create a new WPF application.
  • Drag StackPanel, DockPanel, Popup and Button control on your MianWindow.your window will look like below.

  • Your MainWindow.xaml will look like below.

    <Window x:Class="MainWindow"
        Title="MainWindow" Height="274" Width="337">
            <Popup AllowsTransparency="True" Height="70" HorizontalOffset="1cm" Name="popupWindow"
    Placement="Right" StaysOpen="True" Width="200" >
                <Border BorderBrush="SkyBlue" BorderThickness="2">
                    <DockPanel Background="LemonChiffon" LastChildFill="True">
                        <TextBlock Background="LemonChiffon" DockPanel.Dock="Top"
    FontSize="14" HorizontalAlignment="Stretch"
    Margin="5" Text="My first popup of WPF" />
                        <Button Click="butnExitPopup_Click" Content="Exit"
    DockPanel.Dock="Bottom" Margin="5"
    HorizontalAlignment="Right" MaxHeight="23"/>
                    <Style TargetType="{x:Type Button}">
                        <Setter Property="Margin" Value="2" />
                        <EventSetter Event="Click" Handler="butnDisplayPopup_Click" />
                <Button Content="Display Popup" Name="butnDisplayPopup" />
  • Add the below code in code behind file.

        Private Sub butnExitPopup_Click(sender As Object, e As RoutedEventArgs)
            popupWindow.IsOpen = False
        End Sub
        Private Sub butnDisplayPopup_Click(sender As Object, e As RoutedEventArgs)
            If sender Is butnDisplayPopup Then
                popupWindow.IsOpen = True
            End If
        End Sub

  • Now run your application.





In this article you will learned that how you can close a Popup on Button click.


More Articles

© 2020 DotNetHeaven. All rights reserved.