Bewerken

Delen via


How to open a window or dialog box (WPF .NET)

You can create your own windows and display them in Windows Presentation Foundation (WPF). In this article, you'll learn how to display modal and modeless windows and dialogs.

Conceptually, a window and a dialog box are the same thing: they're displayed to a user to provide information or interaction. They're both "window" objects. The design of the window and the way it's used, is what makes a dialog box. A dialog box is generally small in size and requires the user to respond to it. For more information, see Overview of WPF windows and Dialog boxes overview.

If you're interested in opening operating system dialog boxes, see How to open a common dialog box.

Open as modal

When a modal window is opened, it generally represents a dialog box. WPF restricts interaction to the modal window, and the code that opened the window pauses until the window closes. This mechanism provides an easy way for you to prompt the user with data and wait for their response.

Use the ShowDialog method to open a window. The following code instantiates the window, and opens it modally. The code opening the window pauses, waiting for the window to be closed:

var window = new Margins();

window.Owner = this;
window.ShowDialog();
Dim myWindow As New Margins()

myWindow.Owner = Me
myWindow.ShowDialog()

Important

Once a window is closed, the same object instance can't be used to reopen the window.

For more information about how to handle the user response to a dialog box, see Dialog boxes overview: Processing the response.

Open as modeless

Opening a window modeless means displaying it as a normal window. The code that opens the window continues to run as the window becomes visible. You can focus and interact with all modeless windows displayed by your application, without restriction.

Use the Show method to open a window. The following code instantiates the window, and opens it modeless. The code opening the window continues to run:

var window = new Margins();

window.Owner = this;
window.Show();
Dim myWindow As New Margins()

myWindow.Owner = Me
myWindow.Show()

Important

Once a window is closed, the same object instance can't be used to reopen the window.

See also