TIP: Handling WPF DialogResult with simplicity






3.89/5 (8 votes)
Shorter way to evaluate DialogResult after calling Window.ShowDialog
I have seen many code snippets like:
frm.ShowDialog();
if (frm.DialogResult.HasValue && frm.DialogResult.Value)
{
// do something here
}
orDialogResult result = frm.ShowDialog();
if (result.HasValue && result.Value)
{
// do something here
}
This double check is done because DialogResult
is of nullable type: bool?
, but evaluation can be as simple as:frm.ShowDialog();
if (frm.DialogResult.Equals(true))
{
// do something here
}