Window.DialogResult 属性

定义

获取或设置对话框结果值,此值是从 ShowDialog() 方法返回的值。

public:
 property Nullable<bool> DialogResult { Nullable<bool> get(); void set(Nullable<bool> value); };
[System.ComponentModel.TypeConverter(typeof(System.Windows.DialogResultConverter))]
public bool? DialogResult { get; set; }
[<System.ComponentModel.TypeConverter(typeof(System.Windows.DialogResultConverter))>]
member this.DialogResult : Nullable<bool> with get, set
Public Property DialogResult As Nullable(Of Boolean)

属性值

一个 Nullable<T> 类型的 Boolean 值。 默认值为 false

属性

例外

设置DialogResult 在通过调用 ShowDialog() 打开窗口之前。

- 或 -

通过调用 DialogResult 设置 Show() 至打开的窗口。

示例

以下示例演示如何配置“确定”按钮和“取消”按钮以返回相应的 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()
        {
            InitializeComponent();
        }

        // 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()
            InitializeComponent()
        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) 对话框。 如果接受对话框,则表示打开对话框以检索用户收集的数据并对其进行处理的代码。 但是,如果取消了对话框,则表示调用代码应停止任何进一步的处理。

默认情况下,当用户执行以下操作之一时,将取消对话框:

  • 按“ALT+F4”。

  • 单击“ 关闭 ”按钮。

  • 从“系统”菜单中选择“ 关闭 ”。

在所有这些情况下, DialogResult 默认为 false

对话框通常提供一个用于取消对话框的特殊按钮,该按钮 IsCancel 的 属性设置为 true。 按此方式配置的按钮将在按下或按 ESC 键时自动关闭窗口。 在上述任一情况下, DialogResult 仍为 false

对话框通常还提供接受按钮,即属性设置为 trueIsDefault按钮。 按此方式配置的按钮将在按下或按 ENTER 键时引发其 Click 事件。 但是,它不会自动关闭对话框,也不会将 设置为 DialogResulttrue。 需要手动编写此代码,通常从默认按钮的 Click 事件处理程序中编写。

DialogResultnull 显示对话框但既未接受也不取消时。

对话框关闭后,可以通过 方法返回 ShowDialog 的值或通过检查 DialogResult 属性来获取对话框结果。

DialogResult只能在通过调用其 ShowDialog 方法打开 时Window设置 。

注意

当窗口托管在浏览器中时,无法设置或获取此属性。

适用于