Getting a DialogResult

When interacting with the user, you need to gather information for use later in the application. Dialog windows are a common way to solicit this information.

A frequent scenario in UI applications is gathering information for later use. Dialog windows are a common way to solicit this information. Modal dialogs have an advantage over other types of windows in that the user has to answer your questions before they can continue. This benefit can turn into a curse however if used incorrectly. Anyone who has worked in an application that launches a series of modal dialogs one after another, can attest to the annoyance factor of a poorly written implementation.

WPF doesn't have a separate dialog class. Instead you can treat your window as a normal window or a modal window. Launching a modal dialog is simple in WPF, just call the ShowDialog() method on your window class.

Setting the Dialog result
If you've worked in WinForms prior to moving to WPF you need to learn a few new tricks. That's because the way WPF returns results from a modal dialog is not the same as the Winforms implementation.

Winforms uses the DialogResult enumeration to return the value from a dialog form. Therefore you set the return value to OK, Cancel, Yes, No, Abort, Retry, Ignore or None before returning from the ShowDialog call. In WPF the dialog returns a Nullable instead of an enumeration. Nullable can store three states (null, true, false). Note that you can only assign a value to DialogResult if you open the window with the ShowDialog() method. Also note that the Dialog will always return false unless you write code to return true. Assigning null to DialogResult in the dialog will still cause the return value to be false.

In the following code sample there are two buttons on the dialog. The Yes button has the IsDefault property set to true. IsDefault ties the button to the Enter key, so that pressing the Enter key will fire the Click event. It does not mean that the Yes button will return true for the DialogResult. That is done in the yesButton_Click event procedure.

The No button has the IsCancel property set to true. As a result, pressing the Escape key will close the window. Since the default value for DialogResult is false, that is what is returned when the windows is closed. Another way of saying it; no matter how you close a dialog in WPF it will return false unless you've written code to return true.

Getting the DialogResult
Given what I said in the previous paragraph you might be asking yourself why is the return type of DialogResult typed as Nullable if it is only returning true or false? Because there are other scenarios where you might read the value of DialogResult. One, you could check the value before showing the window. In this case, the DialogResult returns null. Also you could use the Show method instead of the ShowDialog and then test the DialogResult. This also returns null. To review, if you check for the DialogResult and haven't called ShowDialog, you will get null, otherwise you will get back either true or false. Here is some sample code that demonstrates this idea.

This was last published in December 2010

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.