Close a Window in WPF

When a Window is created at run-time using the Window object, it is not visible by default. To make it visible, we can use Show or ShowDialog method.

Show method of Window class is responsible for displaying a window. To open a window, you create an instance of the Window class and call Show method. The following code snippet creates an instance of Window1 class and calls its Show method.

Window1 window = new Window1();

window.Show();

When you use Show method to display a window, this window is modeless window. That means you can switch or activate other windows in the application even when the current window is active window.  The ShowDialog method opens a window as modal window. That means when the current window is active, all other windows on the application are deactivated. You cannot switch to other windows unless the current window is closed. The following code snippet calls the Close method to close a window.

window.ShowDialog();

Close method of the Window class is used to close a window. The following code snippet calls the Close method to close a window.

window.Close();