Various Methods to Close a WPF Application

Various methods to close a WPF Application

This article describes the various methods to close a Windows Presentation Foundation (WPF) Application.

First Create a New Project

  • Start Visual Studio 2012.
  • In the File menu, select "New" -> "Project...".
  • The New Project dialog box appears.

project win.jpg

  • In the Template option, choose Visual C#.
  • From the middle pane, select the WPF Application in Visual C#.
  • Specify the name of your project in the Name TextBox.
  • Specify the location to save the project in the Location TextBox and then click "OK".
  • The MainWindow.xaml designer appears.

mainwin.jpg

  • The App XAML code editor appears; apply the following modifications:

<Window x:Class="WpfApp.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>

</Window>

Creating a Borderless Window

  • We can create a borderless window in two ways.

           First, by writing a WindowStyle property = None, SingleBorderWindow, ThreeDBorderWindow or ToolWindow in the <Window> element.

<Window x:Class="WpfApp1.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" WindowStyle="None">

    <Grid>

    </Grid>

</Window>

The second way is to open the Property Window, select the window style property to either None, SingleBorderWindow, ThreeDBorderWindow or ToolWindow.

window.jpg

Adding a Close Button

  • From the Toolbox add a button (CloseButton) and place it to the top rightmost corner in the MainWindow. Now the Code Editor looks like this:

     

    <Window x:Class="WpfApp1.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" WindowStyle="None">

        <Grid>

            <Button Content="Button" HorizontalAlignment="Left" Margin="491,0,0,0" VerticalAlignment="Top" Width="26" Height="21"/>

        </Grid>

    </Window>

     

  • Now in the Button Content in place of the "Button" place the "X" (close) symbol and set the width and height of the close button and generate the click event for that button.

<Window x:Class="WpfApp1.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" WindowStyle="None">

    <Grid>

        <Button Content="X" Margin="496,0,0,321" Width="21" Height="21" Click="Button_Click"/>

 

    </Grid>

</Window>

cancel.jpg

We can close the window either by using "this.Close()"or by using "App.Current.Shutdown()".

Sometimes "this.close()" does not completely close the application since this method closes the process but sometimes the connection remains open for some other processes.

When the shutdown() method is called the application stops running, since it breaks the connection for all the processes. Hence App.Current.Shutdown is the proper method to close the application. 

  • Use the this.Close() method in the MainWindow.xaml.cs file to close the window.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

 

namespace WpfApp1

{

    ///<summary>

    /// Interaction logic for MainWindow.xaml

    ///</summary>

    publicpartialclassMainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

 

        privatevoid Button_Click(object sender, RoutedEventArgs e)

        {

            this.Close();

        }

    }

}

  • Or use the App.Current.Shutdown() method in the MainWindow.xaml.cs file to close the window.  

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

 

namespace WpfApp1

{

    ///<summary>

    /// Interaction logic for MainWindow.xaml

    ///</summary>

    publicpartialclassMainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

 

        privatevoid Button_Click(object sender, RoutedEventArgs e)

        {

           // this.Close();

            App.Current.Shutdown();

        }

    }

}

 

cancel.jpg

When we click on the "Close" button the window is closed.

Up Next
    Ebook Download
    View all
    Learn
    View all