Can't set DialogResult in WPF

2019-04-03 03:52发布

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;

9条回答
放我归山
2楼-- · 2019-04-03 04:19

I have just had exactly the same problem and it seems to be caused by my overriding the OnClosing() method. I needed to override OnClosing() to stop the user closing the modal window via the close (X) button.

When I comment out the OnClosing() method, the problem goes away and the DialogResult is returned with the expected values of true or false, as set.

For interest here was my button click handlers and OnClosing method:

private void AlternateButton_Click(object sender, RoutedEventArgs e)
{
    this.DialogResult = false;
    buttonHasBeenClicked = true;
    this.Close();
}

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

protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
    base.OnClosing(e);
    if (!buttonHasBeenClicked)
    {
        // Prevent the user closing the window without pressing one of the buttons.
        e.Cancel = true;
    }
}
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-04-03 04:22

I just ran into the problem too. It turns out I had set DialogResult inside a brace of an IF statement and for this reason (as odd as it may seem) caused the error. As soon as this single line was removed, the problem was resolved.

private void OKButton_Click(object sender, RoutedEventArgs e)
    {
        if (!string.IsNullOrEmpty(startBlockPosBox.Text))
        {
          .. do stuff ..
        }
        else
        {
          .. do stuff ..
          DialogResult = true; // this line caused the problem
        }

        DialogResult = true;
    }
查看更多
We Are One
4楼-- · 2019-04-03 04:25

I have the following in the dialog window page. (dialogwindow.xaml.cs)

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

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

In the calling page I used the dialogwindow like this:

dialogwindow dWinObj = new dialogwindow();
if(dWinObj.ShowDialog().Value == true)
{
  //perform the operation when the user clicks "Yes"
}
查看更多
登录 后发表回答