I am currently working at Uniblue Systems as a .Net Specialist. I love C# and addicted to the WPF technology…. In my blog (http://marlongrech.wordpress.com/) you will find posts mainly on WPF yet sometimes I blog on other .Net stuff…. I also have an OpenSource project AvalonControlsLibrary(http://marlongrech.wordpress.com/) that I constantly work on. Marlon has posted 12 posts at DZone. View Full User Profile

dotNET WPF Dialogs and DialogResult

06.06.2008
| 10465 views |
  • submit to reddit

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: 

DialogResult result = new Form1().ShowDialog();
if (result == DialogResult.OK)
MessageBox.Show(“User clicked OK”);
else if (result == DialogResult.Cancel)
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: 

MyDialog dialog = new MyDialog();
dialog.ShowDialog();
if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
MessageBox.Show(“User clicked OK”);
else
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: 

<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: 

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

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

References
Published at DZone with permission of its author, Marlon Grech. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)