.net 如何让 System.Windows.ShowDialog() 返回“true”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/505572/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 11:09:24  来源:igfitidea点击:

How do I get System.Windows.ShowDialog() to return 'true'?

.netwpfdialog

提问by Alex Baranosky

How do I get System.Windows.ShowDialog()to return 'true'?

我如何System.Windows.ShowDialog()才能返回“真实”?

I am a little new to this. System.Windows.ShowDialog's return type is bool? It is supposed to return truewhen you hit Submit, and falsewhen you hit Cancel. But I am not sure how to designate which Buttonis the official submit button.

我对此有点陌生。 System.Windows.ShowDialog的返回类型是bool?它应该返回true,当你打Submit,而false当你打Cancel。但我不确定如何指定哪个Button是官方提交按钮。

EDIT: On a related note, I am curious as to how it can return null.

编辑:在相关说明中,我很好奇它如何返回 null。

回答by JMD

http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog.aspx

http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog.aspx

ShowDialog returns a Nullable<(Of <(T>)>) Boolean value that specifies whether the activity was accepted or canceled. The return value is the value of the DialogResult property before a window closes (see DialogResult).

ShowDialog 返回一个 Nullable<(Of <(T>)>) 布尔值,该值指定活动是被接受还是被取消。返回值是窗口关闭之前 DialogResult 属性的值(请参阅 DialogResult)。

Basically, you decide by setting the value of the DialogResult, not by hitting a particular button -- you decide what the button does.

基本上,您通过设置 DialogResult 的值来决定,而不是通过点击特定按钮来决定——您决定按钮的作用。

回答by Matt Hamilton

In WPF, set the Button.IsDefaultproperty to true to specify that a button is the "submit" button for a window. I'm not 100% sure that this will make the window close with a DialogResult of true. If it doesn't, you just need to handle its Click event thusly:

在 WPF 中,将Button.IsDefault属性设置为 true 以指定按钮是窗口的“提交”按钮。我不是 100% 确定这将使窗口以 DialogResult 为真关闭。如果没有,你只需要这样处理它的 Click 事件:

this.DialogResult = true;

Edit

编辑

Likewise, you can use the Button.IsCancelproperty to have a button be the "cancel" button for a form.

同样,您可以使用Button.IsCancel属性使按钮成为表单的“取消”按钮。

Edit 2

编辑 2

I believe the reason ShowDialog is nullable is that since it's null up until the form is submitted or canceled, you could test for that if you were watching the dialog in a background thread. I haven't tried that, but it seems like a logical reason why they'd introduce a third "unknown" (null) state to the property.

我相信 ShowDialog 可以为空的原因是因为在提交或取消表单之前它是空的,如果您在后台线程中观看对话框,您可以测试它。我还没有尝试过,但这似乎是他们向属性引入第三个“未知”(空)状态的合乎逻辑的原因。

回答by Nir

if you set DialogResult to true ShowDialog returns true, if you set DialogResult to false ShowDialog returns false if the dialog is closed without setting DialogResult (the user clicks on the red X in the top right corner) ShowDialog will return null.

如果将 DialogResult 设置为 true ShowDialog 返回 true,如果将 DialogResult 设置为 false ShowDialog 返回 false 如果对话框在没有设置 DialogResult 的情况下关闭(用户单击右上角的红色 X) ShowDialog 将返回 null。

Setting IsDefault to true will cause the button to look a little different and pressing enter will "click" this button.

将 IsDefault 设置为 true 将导致按钮看起来有点不同,按 Enter 键将“单击”此按钮。

If you set IsCancel to true the pressing esc will "click" this button.

如果您将 IsCancel 设置为 true,则按 esc 将“单击”此按钮。