C# Disciples

my life in Avalon ….

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

May 28, 2008 - Posted by | WPF

13 Comments »

  1. Thanks!!!

    Comment by Cornel | May 29, 2008 | Reply

  2. [...] WPF Dialogs and DialogResult (Marlon Grech) [...]

    Pingback by Dew Drop - May 29, 2008 | Alvin Ashcraft's Morning Dew | May 29, 2008 | Reply

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

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

    ..is the same as..

    if(dialog.DialogResult == true)

    Comment by PyroSpirit | October 21, 2008 | Reply

  4. 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.

    Comment by toddpi314 | January 25, 2009 | Reply

  5. I use live writer… for me it is the best tool around!

    Comment by marlongrech | January 26, 2009 | Reply

  6. that works perfect! thanks!!!

    Comment by brenda | March 10, 2009 | Reply

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

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

    Comment by Nils | July 3, 2009 | Reply

  8. Very usefull to me.
    Thanks.

    Comment by Jonathan | October 13, 2009 | Reply

  9. Thanks for nice blog! I think it’s useful. http://www.wowebook.co.cc/net/wpf-recipes-in-c-2008-a-problem-solution-approach/

    Comment by Lee2010 | September 9, 2010 | Reply

  10. You have to update your article.

    Change DialogResult to bool?

    Comment by Dimas | January 18, 2012 | Reply

    • The first example is for a Forms Dialog and is correct.

      Comment by Adrian | March 15, 2012 | Reply

  11. 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.

    Comment by Earl Hickey | April 12, 2012 | Reply

  12. So typical of Microsoft, take what’s already been used by many a user and invent a knew, counterintuitive and hairbrained approach.

    Comment by Earl Hickey | April 12, 2012 | Reply


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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 845 other followers

%d bloggers like this: