How do I get System.Windows.ShowDialog() to return 'true'?

http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog.aspx

ShowDialog returns a Nullable<Boolean> value that specifies whether the activity was accepted or canceled. The return value is the value of the DialogResult property before a window closes (see DialogResult).

Basically, you decide by setting the value of the DialogResult, not by hitting a particular button -- you decide what the button does.


In WPF, set the Button.IsDefault property to true to specify that a button is the "submit" button for a window. I'm not 100% sure that this will make the window close with a DialogResult of true. If it doesn't, you just need to handle its Click event thusly:

this.DialogResult = true;

Edit

Likewise, you can use the Button.IsCancel property to have a button be the "cancel" button for a form.

Edit 2

I believe the reason ShowDialog is nullable is that since it's null up until the form is submitted or canceled, you could test for that if you were watching the dialog in a background thread. I haven't tried that, but it seems like a logical reason why they'd introduce a third "unknown" (null) state to the property.


if you set DialogResult to true ShowDialog returns true, if you set DialogResult to false ShowDialog returns false if the dialog is closed without setting DialogResult (the user clicks on the red X in the top right corner) ShowDialog will return null.

Setting IsDefault to true will cause the button to look a little different and pressing enter will "click" this button.

If you set IsCancel to true the pressing esc will "click" this button.

Tags:

.Net

Wpf

Dialog