C# 无法在 WPF 中设置 DialogResult
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/898708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't set DialogResult in WPF
提问by quinnapi
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 按钮的单击事件中,我分别设置了 this.DialogResult = true (或 false),并且未设置该值。窗口按预期关闭,但 DialogResult 仍为空。
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 中的错误吗?或者是否存在无法设置 DialogResult 属性但不引发异常的原因?该窗口未托管在浏览器中。
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;
回答by Carlo
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?
至于它是空的......我不明白为什么会发生这种情况,除非它在被设置为真或假之后以某种方式被设置回空。你能展示你的代码吗?
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?
有什么大的区别吗?
回答by Dennis
DialogResult
is a nullable bool. However you do not have to cast it to get it's value.
DialogResult
是一个可为空的布尔值。但是,您不必强制转换它以获得它的价值。
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.
至于最初的问题,我唯一一次看到并跟踪此问题是在设置 DialogResult 和关闭窗口之间处理窗口时。不幸的是,我可以提供的唯一建议是让您逐步执行代码并检查操作顺序。我相信我通过设置DialogResult
然后显式关闭窗口来“修复”它。
回答by batzen
Do you close the window before u set the DialogResult? You should post the whole content of your button event-handlers.
在设置 DialogResult 之前是否关闭窗口?您应该发布按钮事件处理程序的全部内容。
回答by dodgy_coder
I have just had exactlythe 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.
我刚刚遇到了完全相同的问题,它似乎是由我覆盖 OnClosing() 方法引起的。我需要覆盖 OnClosing() 以阻止用户通过关闭 (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() 方法时,问题就消失了,DialogResult 返回了预期的 true 或 false 值,如设置。
For interest here was my button click handlers and OnClosing method:
感兴趣的是我的按钮单击处理程序和 OnClosing 方法:
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;
}
}
回答by Ehsan Zargar Ershadi
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 :
设置我的 DialogResult 后,它对我有用!!(非常奇怪的问题)。这是我使用的代码:
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;
}
回答by dracore
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 语句的大括号内设置了 DialogResult,因此(尽管看起来很奇怪)导致了错误。一旦删除了这一行,问题就解决了。
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;
}
回答by Kishore
I have the following in the dialog window page. (dialogwindow.xaml.cs)
我在对话框窗口页面中有以下内容。(对话框窗口.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"
}
回答by Christopher Jordan
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:
我有一个类似的问题,但我的问题来自我的结束语中的代码。我试图在窗口关闭之前 Dispose() 一个 List,然后将 List<> 属性设置为 null...在我的 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();
}
}
}
回答by jlo-gmail
The problem is due to the life of the form:
问题是由于表单的生命周期:
Dialog eventprivate void _loginViewModel_LoginEvent(object sender, LoginViewModel.LoginEventArgs e) { DialogResult = true; this.Close(); }
对话事件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();