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.
  • 4097
Download Files:
 

Introduction
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.

    popup6.gif
     
  • Your MainWindow.xaml will look like below.

    <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="274" Width="337">
        <StackPanel>
            <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"/>
                    </DockPanel>
                </Border>
            </Popup>
            <StackPanel>
                <StackPanel.Resources>
                    <Style TargetType="{x:Type Button}">
                        <Setter Property="Margin" Value="2" />
                        <EventSetter Event="Click" Handler="butnDisplayPopup_Click" />
                    </Style>
                </StackPanel.Resources>
                <Button Content="Display Popup" Name="butnDisplayPopup" />
            </StackPanel>
        </StackPanel>
    </
    Window>
     
  • 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.
Output

popup2.gif

popup3.gif

popup4.gifpopup5.gif

popup2.gif

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

Categories

More Articles

© 2013 dotNetheaven. All rights reserved.