Occurs directly after Close is called, and can be handled to cancel window closure.
Assembly: PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
can be handled to detect when a window is being closed (for example, when Close is called). Furthermore, can be used to prevent a window from closing. To prevent a window from closing, you can set the Cancel property of the CancelEventArgs argument to true.
The event is raised when Close is called, if a window's Close button is clicked, or if the user presses ALT+F4.
If an owned window was opened by its owner window using Show, and the owner window is closed, the owned window's event is not raised. If the owner of a window is closed (see Owner), is not raised on the owned window.
If Shutdown is called, the event for each window is raised. However, if is canceled, cancellation is ignored.
If a session ends because a user logs off or shuts down, is not raised; handle SessionEnding to implement code that cancels application closure.
If you want to show and hide a window multiple times during the lifetime of an application, and you don't want to reinstantiate the window each time you show it, you can handle the event, cancel it, and call the Hide method. Then, you can call Show on the same instance to reopen it.
The following example demonstrates a Window that determines whether it needs user intervention to close.
Imports System ' EventArgs Imports System.ComponentModel ' CancelEventArgs Imports System.Windows ' window Namespace VisualBasic Partial Public Class DataWindow Inherits Window ' Is data dirty Private isDataDirty As Boolean = False ... Private Sub DataWindow_Closing(ByVal sender As Object, ByVal e As CancelEventArgs) MessageBox.Show("Closing called") ' If data is dirty, notify user and ask for a response If Me.isDataDirty Then Dim msg As String = "Data is dirty. Close without saving?" Dim result As MessageBoxResult = MessageBox.Show(msg, "Data App", MessageBoxButton.YesNo, MessageBoxImage.Warning) If result = MessageBoxResult.No Then ' If user doesn't want to close, cancel closure e.Cancel = True End If End If End Sub End Class End Namespace
Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)