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





SHIVSHANKER HANDEPosted Nov 7, 2017, 8:11 AM
Please can you explain for MVVM pattern