ARTICLE

How to Close Popup on Button click in WPF using VB.NET

Posted by Shalini Juneja Articles | WPF using VB.NET May 27, 2011
In this article we demonstrate on how to close a Popup on Button click.which work as a Container.
Download Files:
 
Reader Level:


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.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Diagram
Become a Sponsor