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

project win.jpg

mainwin.jpg

<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

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

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

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();

}

}

}

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.