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 可以從顯示對話方塊的程式碼中使用,以判斷使用者是否接受 () , truefalse) 對話方塊中取消 (。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.