BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

WPF Controls - Window - Showing Dialog Boxes

The one hundred and seventy-first part of the Windows Presentation Foundation Fundamentals tutorial continues to describe the Window class. This article describes the process for displaying custom dialog boxes.

Showing a Dialog Box

As mentioned above, you display a window as a dialog box by calling its ShowDialog method. This is similar to the Show method, except that processing in the calling code stops until the dialog box is closed. This allows the ShowDialog method to have a return value, which tells you whether the dialog box was accepted or cancelled.

The return value is a nullable Boolean value. If the user accepts the information, this should be set to true. If the user cancels the dialog box, it should be false. A null value indicates neither situation, allowing you to set a third option where necessary. For example, you might create a dialog box to control Printing. You could include a Print button to perform the print, returning true. A Cancel button could close the window and return false. A Close button might cancel the print but return null, indicating that any paper settings that were changed should be retained.

Let's update the Open button in the main window so that it launches a new dialog box and reacts to the information entered. Start by modifying the XAML of the button to register the Click event:

<Button Grid.Row="2" Grid.Column="1" Margin="5" Click="Open_Click">Open</Button>

In the method behind the button we'll create a new CustomDialogBox object and display it using the ShowDialog method. We'll capture the result. If it is true, meaning the user clicked the OK button, we'll set the text in the two key TextBlocks to indicate the button that was clicked and the text entered in the dialog box. We'll use default text for any other result.

Add the following method behind the main window:

private void Open_Click(object sender, RoutedEventArgs e)
{
    var dialog = new CustomDialogBox();
    bool? result = dialog.ShowDialog();

    if (result == true)
    {
        ButtonClicked.Text = "OK";
        TextEntered.Text = dialog.MyTextBox.Text;
    }
    else if (result == false)
    {
        ButtonClicked.Text = "Cancel / Close";
        TextEntered.Text = "";
    }
    else
    {
        ButtonClicked.Text = "n/a";
        TextEntered.Text = "n/a";
    }
}

Controlling the Dialog Box Return Value

Now we can turn our attention to the dialog box window. Setting the IsCancel property of a button has additional effects when used in a dialog box. If the user clicks the button, the window closes automatically and the result of the ShowDialog method is set to false. This means that we do not have to add any extra code to the Cancel button.

The IsDefault option has no extra functionality, as it is quite possible that the default button should not close the dialog box. For our OK button, we'll need to capture the Click event and close the window from code. We'll also need to set the value to return from the method, using the DialogResult property of the window.

Register the Click event by updating the XAML for the OK button:

<Button Grid.Row="1" IsDefault="True" Margin="2" Click="OK_Click">OK</Button>

When the OK button is clicked, we'll set the DialogResult property to true and close the window. Add the following method behind the window:

private void OK_Click(object sender, RoutedEventArgs e)
{
    DialogResult = true;
    Close();
}

Run the program to test it works correctly. Click the Open button to show the dialog box. Note that you cannot switch back to the main window whilst it is visible. Try entering text and clicking OK or Cancel to see the information transferred back to the main window.

8 July 2015