Window.DialogResult 属性


获取或设置对话框结果值,此值是从 ShowDialog() 方法返回的值。Gets or sets the dialog result value, which is the value that is returned from the ShowDialog() method.

 property Nullable<bool> DialogResult { Nullable<bool> get(); void set(Nullable<bool> value); };
public bool? DialogResult { get; set; }
member this.DialogResult : Nullable<bool> with get, set
Public Property DialogResult As Nullable(Of Boolean)



一个 Nullable<T> 类型的 Boolean 值。A Nullable<T> value of type Boolean. 默认值为 falseThe default is false.



设置DialogResult 在通过调用 ShowDialog() 打开窗口之前。DialogResult is set before a window is opened by calling ShowDialog().

- 或 --or- 通过调用 DialogResult 设置 Show() 至打开的窗口。DialogResult is set on a window that is opened by calling Show().


下面的示例演示如何配置 "确定" 按钮和 "取消" 按钮以返回相应的 DialogResultThe following example shows how to configure an OK button and a Cancel button to return the appropriate DialogResult.

<Button IsDefault="True" Click="acceptButton_Click">OK (IsDefault=True)</Button>
<Button IsCancel="True">Cancel (IsCancel=True)</Button>
using System;
using System.Windows;
using System.Windows.Controls;

namespace CSharp
    public partial class DialogBox : Window
        public DialogBox()

        // The accept button is a button whose IsDefault property is set to true.
        // This event is raised whenever this button is clicked, or the ENTER key
        // is pressed.
        void acceptButton_Click(object sender, RoutedEventArgs e)
            // Accept the dialog and return the dialog result
            this.DialogResult = true;

Imports System.Windows
Imports System.Windows.Controls

Namespace VisualBasic
    Partial Public Class DialogBox
        Inherits Window
        Public Sub New()
        End Sub

        ' The accept button is a button whose IsDefault property is set to true.
        ' This event is raised whenever this button is clicked, or the ENTER key
        ' is pressed.
        Private Sub acceptButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            ' Accept the dialog and return the dialog result
            Me.DialogResult = True
        End Sub
    End Class
End Namespace


DialogResult 可从显示对话框的代码中使用,以确定用户是否接受 (true) 或取消 (false) 对话框。DialogResult can be used from the code that showed a dialog box to determine whether a user accepted (true) or canceled (false) the dialog box. 如果接受了某个对话框,这将表示打开此对话框的代码以检索用户收集的数据并对其进行处理。If a dialog box was accepted, this signifies to the code that opened the dialog box to retrieve the data that was collected by the user and process it. 不过,如果已取消对话框,这表示调用代码应停止任何进一步的处理。If a dialog box was canceled, however, this signifies that calling code should stop any further processing.

默认情况下,当用户执行以下操作之一时,将取消对话框:By default, a dialog box is canceled when a user does one of the following:

  • PressesALT + F4。PressesALT+F4.

  • 单击 " 关闭 " 按钮。Clicks the Close button.

  • 从 "系统" 菜单中选择 " 关闭 "。Selects Close from the System menu.

在所有这些情况下, DialogResult false 默认为。In all of these cases, DialogResult is false by default.

对话框通常提供取消对话框(其属性设置为的按钮)的特殊按钮 IsCancel trueA dialog box typically provides a special button to cancel a dialog, which is the button whose IsCancel property is set to true. 按此方式配置的按钮会在按下或按 ESC 键时自动关闭窗口。A button configured this way will automatically close a window when either it is pressed, or when the ESC key is pressed. 在这两种情况下,都将 DialogResult 保留 falseIn either of these cases, DialogResult remains false.

对话框通常还会提供 "接受" 按钮,该按钮是其 IsDefault 属性设置为的按钮 trueA dialog box also typically provides an accept button, which is the button whose IsDefault property is set to true. 按此方式配置的按钮会 Click 在按下或按下 ENTER 键时引发其事件。A button configured this way will raise its Click event when either it or the ENTER key is pressed. 但是,它不会自动关闭对话框,也不会将其设置 DialogResulttrueHowever, it won't automatically close the dialog box, nor will it set DialogResult to true. 你需要手动编写此代码,通常来自 Click 默认按钮的事件处理程序。You need to manually write this code, usually from the Click event handler for the default button.

DialogResultnull显示对话框时,但不接受也不取消。DialogResult is null when the dialog box is shown but neither accepted nor canceled.

对话框关闭后,可以从方法返回的值 ShowDialog 或通过检查属性来获取对话框结果 DialogResultAfter a dialog box closes, you can get the dialog result from the value returned by ShowDialog method, or by inspecting the DialogResult property.

DialogResult 只能在 Window 通过调用其方法打开时设置 ShowDialogDialogResult can only be set when a Window is opened by calling its ShowDialog method.


当窗口承载于浏览器中时,不能设置或获取此属性。You cannot set or get this property when a window is hosted in a browser.