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

I had a similar issue, but my issue came from the code within my closing statement. I was trying to Dispose() a List before the window closed, and then set the List<> property to null... it was choking on the set property when I was trying to set its value to null so I came up with the following clumsy workaround in my set property method and everything worked afterward:

    List<SettingItem> settingItems;
    public IEnumerable<SettingItem> Settings
    {
        get
        {
            return settingItems.OrderBy(t => t.Name).AsEnumerable();
        }
        set
        {
            if (value == null)
            {
                settingItems.Clear();
            }
            else
            {
                settingItems = value.ToList();
            }
        }
    }
查看更多
男人必须洒脱
3楼-- · 2019-04-03 04:05

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?

查看更多
不美不萌又怎样
4楼-- · 2019-04-03 04:08

The problem is due to the life of the form:

Dialog event private void _loginViewModel_LoginEvent(object sender, LoginViewModel.LoginEventArgs e) { DialogResult = true; this.Close(); }

Works:

var login = new Login();
var result = login.ShowDialog();

Does NOT work:

var result = new Login().ShowDialog();
查看更多
劫难
5楼-- · 2019-04-03 04:10

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 question, the only time I have seen and traced this issue is when the window was being disposed between setting the DialogResult and closing the window. Unfortunately the only advice that I can offer is for you step through your code and check the order of the operations. I believe that I "fixed" it by setting the DialogResult and then explicitly closing the window.

查看更多
贼婆χ
6楼-- · 2019-04-03 04:10

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

查看更多
Lonely孤独者°
7楼-- · 2019-04-03 04:11

I have been into this problem too, and the only way i have found to fix it was using this code in my Class :

public new bool? DialogResult { get; set; }

and after setting my DialogResult it work out for me !! ( very strange issue ). this was the code i was using :

cmdCancel = new RelayCommand(() => { DataContact.Reload(); this.DialogResult = false; this.Close(); });

and to open my dialog :

public static MessageBoxResult ShowQuestionYesNo(string message)
        {
            POLMessageBox w = new POLMessageBox("سوال", MessageBoxType.QuestionYesNo, message);
            w.ShowDialog();
            var b = w.DialogResult;
            if (b == true) return MessageBoxResult.Yes;
            if (b == false) return MessageBoxResult.No;
            return MessageBoxResult.No;
        }
查看更多
登录 后发表回答