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.
我将在调用窗口中使用ShowDialog()显示WPF窗口。窗口打开并按预期模式运行。但是,在对话框窗口的OK和Cancel按钮的单击事件中,我设置了这个。对话框分别为true(或false),且值没有设置。窗口按预期关闭,但对话框仍然为空。
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.
这是WPF中的一个bug吗?或者是因为不能设置对话框属性而没有抛出异常吗?窗口不驻留在浏览器中。
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;
8 个解决方案
#1
18
DialogResult
is a nullable bool. However you do not have to cast it to get it's value.
对话框是不可对话的bool。但是,您不必强制转换它以获得它的值。
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)
的? ?设置如果结果为空,则返回的默认值。更多信息:使用可空类型(c#编程指南)
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.
至于最初的问题,我唯一一次看到并跟踪这个问题的时间是在设置对话框和关闭窗口之间处理窗口的时间。不幸的是,我所能提供的唯一建议是您逐步检查代码并检查操作的顺序。我认为我通过设置对话框,然后显式地关闭窗口来“修复”它。
#2
9
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 (bool?),所以为了比较它或将它设置为另一个变量,你必须把它转换成常规的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?
因为它是空的…我不明白为什么会发生这种情况,除非它在被设置为真或假后被重新设置为null。你能展示你的代码吗?
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:
而在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?
有什么大的区别吗?
#3
6
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.
我刚刚遇到了完全相同的问题,它似乎是由覆盖onclose()方法引起的。我需要重写onclose()以阻止用户通过close (X)按钮关闭模式窗口。
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.
当我注释掉OnClosing()方法时,问题就消失了,并返回对话框,其期望值为true或false。
For interest here was my button click handlers and OnClosing method:
感兴趣的是我的按钮点击处理程序和onclose方法:
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;
}
}
#4
3
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;
}
#5
2
Do you close the window before u set the DialogResult? You should post the whole content of your button event-handlers.
在你开始讲话之前,你把窗户关上吗?您应该发布按钮事件处理程序的全部内容。
#6
0
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.
我也遇到了这个问题。结果证明,我在IF语句的两个括号中设置了对话框,出于这个原因(尽管看起来很奇怪)导致了错误。一旦这一行被删除,问题就解决了。
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;
}
#7
0
I have the following in the dialog window page. (dialogwindow.xaml.cs)
对话框窗口页面中有如下内容。(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"
}
#8
-1
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:
我也遇到过类似的问题,但我的问题来自我结语中的代码。我试图在窗口关闭之前处理一个列表,然后将列表<>属性设置为null…当我试图将它的值设为null时,它被set属性阻塞了,所以我在set属性方法中想出了以下笨拙的解决方案,之后一切都成功了:
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();
}
}
}
#1
18
DialogResult
is a nullable bool. However you do not have to cast it to get it's value.
对话框是不可对话的bool。但是,您不必强制转换它以获得它的值。
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)
的? ?设置如果结果为空,则返回的默认值。更多信息:使用可空类型(c#编程指南)
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.
至于最初的问题,我唯一一次看到并跟踪这个问题的时间是在设置对话框和关闭窗口之间处理窗口的时间。不幸的是,我所能提供的唯一建议是您逐步检查代码并检查操作的顺序。我认为我通过设置对话框,然后显式地关闭窗口来“修复”它。
#2
9
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 (bool?),所以为了比较它或将它设置为另一个变量,你必须把它转换成常规的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?
因为它是空的…我不明白为什么会发生这种情况,除非它在被设置为真或假后被重新设置为null。你能展示你的代码吗?
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:
而在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?
有什么大的区别吗?
#3
6
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.
我刚刚遇到了完全相同的问题,它似乎是由覆盖onclose()方法引起的。我需要重写onclose()以阻止用户通过close (X)按钮关闭模式窗口。
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.
当我注释掉OnClosing()方法时,问题就消失了,并返回对话框,其期望值为true或false。
For interest here was my button click handlers and OnClosing method:
感兴趣的是我的按钮点击处理程序和onclose方法:
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;
}
}
#4
3
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;
}
#5
2
Do you close the window before u set the DialogResult? You should post the whole content of your button event-handlers.
在你开始讲话之前,你把窗户关上吗?您应该发布按钮事件处理程序的全部内容。
#6
0
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.
我也遇到了这个问题。结果证明,我在IF语句的两个括号中设置了对话框,出于这个原因(尽管看起来很奇怪)导致了错误。一旦这一行被删除,问题就解决了。
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;
}
#7
0
I have the following in the dialog window page. (dialogwindow.xaml.cs)
对话框窗口页面中有如下内容。(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"
}
#8
-1
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:
我也遇到过类似的问题,但我的问题来自我结语中的代码。我试图在窗口关闭之前处理一个列表,然后将列表<>属性设置为null…当我试图将它的值设为null时,它被set属性阻塞了,所以我在set属性方法中想出了以下笨拙的解决方案,之后一切都成功了:
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();
}
}
}