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 () 對話方塊。 如果已接受對話方塊,這表示開啟對話方塊的程式碼,以擷取使用者所收集的資料並加以處理。 不過,如果對話方塊已取消,這表示呼叫程式碼應該停止任何進一步的處理。

根據預設,當使用者執行下列其中一項動作時,就會取消對話方塊:

  • PressesALT+F4。

  • 按一下 [ 關閉] 按鈕。

  • 從 [系統] 功能表選取 [ 關閉 ]。

在這些情況下, DialogResult 預設為 false

對話方塊通常會提供特殊的按鈕來取消對話方塊,也就是屬性設定為 trueIsCancel 按鈕。 以這種方式設定的按鈕會在按下視窗或按下 ESC 鍵時自動關閉視窗。 在上述任一案例中, DialogResult 都會維持 false

對話方塊通常也會提供接受按鈕,也就是屬性設定為 trueIsDefault 按鈕。 以這種方式設定的按鈕會在按下或按下 ENTER 鍵時引發其 Click 事件。 不過,它不會自動關閉對話方塊,也不會將其設定 DialogResulttrue 。 您需要手動撰寫此程式碼,通常是來自 Click 預設按鈕的事件處理常式。

DialogResultnull 顯示對話方塊,但未接受或取消。

對話方塊關閉之後,您可以從 方法傳 ShowDialog 回的值,或是檢查 DialogResult 屬性,取得對話方塊結果。

DialogResult 只有在藉由呼叫 其 方法開啟 時 WindowShowDialog ,才能設定 。

注意

當視窗裝載在瀏覽器中時,您無法設定或取得這個屬性。

適用於