views:

4262

answers:

4

I show a WPF window using ShowDialog() from the calling window. The window opens and is modal as expected. However, in my OK and Cancel button's click events in the dialog window I set this.DialogResult = true (or false) respectively, and the value does not get set. The window closes as expected, but DialogResult is still null.

Is this a bug in WPF? Or is there a reason the DialogResult property cannot be set yet does not throw an exception? The window is not hosted in a browser.

Code in the calling window:

Window2 win = new Window2();
bool? result = win.ShowDialog();
if (result.HasValue && result.Value) {
   //never gets here because result is always null
}

Code in the dialog window:

this.DialogResult = true;
+1  A: 

Is the window hosted in a browser? If so, you can't set that property for this window.

Moayad Mardini
The window is not hosted in a browser.
quinnapi
+2  A: 

Well first of all you have to take into account that it returns a nullable bool (bool?), so in order to compare it or set it to another variable you have to cast it to a regular bool

bool result = (bool)myWindow.DialogResult;

As for it being null... I don't see why that should happen, unless it's somehow being set back to null AFTER being set to true or false. Can you show your code?

EDIT:

Your code worked fine for me, this is what I have in the second window:

private void button2_Click(object sender, RoutedEventArgs e)
{
    this.DialogResult = false;
}

private void button1_Click(object sender, RoutedEventArgs e)
{
    this.DialogResult = true;
}

And in Window1:

private void window1_Loaded(object sender, RoutedEventArgs e)
{
    Window2 win = new Window2();

    bool? result = win.ShowDialog();

    if (result.HasValue && result.Value)
    {
     //it DID get here
    }
}

Is there any big difference?

Carlo
In a debugging session, I check the value of DialogResult immediately after setting it to true and it is still null.
quinnapi
Hhmmm, can you post your code?
Carlo
No difference. In fact, I do the exact same thing with several other windows in the same project, and they all work fine.
quinnapi
Try using bool? result = win.DialogResult; AFTER win.ShowDialog()... I know it's not promising but let's try. Is there any more info you think I might need to recreate the scenario? This is some odd behavior...
Carlo
+3  A: 

DialogResult is a nullable bool. However you do not have to cast it to get it's value.

bool? result = myWindow.ShowDialog();
if (result ?? false)
{
  // snip
}

The ?? sets the default value to return if the result is null. More information: Using Nullable Types (C# Programming Guide)

As for the original issue, the only time I have seen this is when the window is being disposed between you setting the DialogResult/closing the window and you testing the dialog result. There is not much I can do to help except point you in this direction and tell you to debug it.

HTH,

Dennis Roche
The window is not being disposed at that time.
quinnapi
sorry for not following up sooner. if you are experiencing this issue it is either: a) you are not setting the dialog result, b) you ARE disposing the window. I think if you call window.Close() or this.Close() it will dispose the window and reset the value DialogResult to NULL.
Dennis Roche
that's right. A friend of mine uses result.Value in order to get true or false.
Junior Mayhé
+2  A: 

Do you close the window before u set the DialogResult? You should post the whole content of your button event-handlers.

batzen
I think this is right. The only way we will solve this is to see more context.
PeterAllenWebb