WPF Dialogs and DialogResult

In WPF Dialogs are quite different from Windows Forms. The behavior is still the same i.e when you have a Dialog opened (by calling the ShowDialog() method) the user must close the dialog in order to use the Window that opened the Dialog Window. The difference is the way you handle the Dialog Result and also how to set Dialog Results.

Let’s say that you created a Window in Windows Forms and you have a Button. You can set the DialogResult property of the button so that when the user click on that button the Dialog ends with the Dialog Result that you have set. (also if the user clicks the exit button of the Dialog the DialogResult would be a Cancel). The code to handle the DialogResult in Windows Forms would look like this

   1: DialogResult result = new Form1().ShowDialog();
   2: if (result == DialogResult.OK)
   3:     MessageBox.Show("User clicked OK");
   4: else if (result == DialogResult.Cancel)
   5:     MessageBox.Show("User clicked Cancel");

In the WPF this is different. There is no DialogResult property on controls and also the ShowDialog does not return a DialogResult instead it returns a Nullable<bool>.

So to handle a DialogResult after calling the ShowDialog() in WPF your code would look like this

   1: MyDialog dialog = new MyDialog();
   2: dialog.ShowDialog();
   3:
   4: if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
   5:     MessageBox.Show("User clicked OK");
   6: else
   7:     MessageBox.Show("User clicked Cancel");

ShowDialog can return True which is equivalent to DialogResult.Ok or False which is equivalent to DialogResult.Cancel.

So let’s have a look at how you can create a Dialog in WPF and set the DialogResult of the Dialog. We can do this by setting a property on the Window called DialogResult. Once you set this property on the Window the Window will automatically close and the ShowDialog methods returns the result that you have set in the DialogResult property.

If you want to have a “Cancel” Button for your Dialog you can use the IsCancel property and set this to true. When the user clicks the button the DialogResult will be set to false and the Dialog will close. This will also allow the user to click ESC to cancel the dialog ( which is something that every user would expect from a dialog). Please note that if you have more than one button with the IsCancel = “True” the ESC does not work as expected instead the focus will be given to the first button that has the IsCancel = “True”. The XAML for this button would look like this

   1: <Button Width="100" Content="Cancel" IsCancel="True"/>

If you want to have an “Ok” button you can set the IsDefault property. This property will NOT set the DialogResult to true for you, instead it will allow the user to click “Enter” and the Click event handler of the button is automatically called (so as such it doesn’t really have to do with Dialogs, it is just more useful when you are using Dialogs because users would expect such a behavior). You must code the event handler (and obviously register the handler to the click event) yourself and set the DialogResult to true. Something like this

   1: private void ButtonOkClick(object sender, RoutedEventArgs e)
   2: {
   3:     DialogResult = true;
   4: }

That’s basically it. For more information you might want to have a look at the MSDN documentation on this here.

Download Sample Dialog application

About these ads

20 thoughts on “WPF Dialogs and DialogResult

  1. Pingback: Dew Drop - May 29, 2008 | Alvin Ashcraft's Morning Dew

  2. You can simplify that if statement slightly for conciseness:

    if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)

    ..is the same as..

    if(dialog.DialogResult == true)

  3. Sweet post. By the way, what are you using to edit your WordPress Blog?

    I was looking for an easy way to drop in SkyDrive code links, and Live Writer looks like a good option.

  4. to:
    dialog.DialogResult.HasValue && dialog.DialogResult.Value

    much more readable i think is:
    dialog.DialogResult.GetValueOrDefault(false);

  5. I wish i knew why they changed the old Enum-based approach, and used a bool return value.
    This makes no sense, and I even have to handle the click event in the latter (IsDefault) case.

  6. Hello there, You’ve done an incredible job. I’ll
    definitely digg it and personally recommend to my friends.
    I’m confident they will be benefited from this web site.

  7. Pingback: ShowDialog: Memory leak issue | CuttingEdgeTechnologies

  8. Dear marlongrech,

    I’ve spent rough 12 and half hours so far at work today, working on one of my pojects and I’ve been trying to achieve a dialog box with results, and struggled with most peoples suggestions on other posts. I saw yours, and thought how can this work…there’s hardly anything to it! It works brilliantly. I’ve adapted it also with properties on my dialog, to make use of wpf messageboxresults, and used custom icons and texts etc.

    Thanks for your blog post!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s