Window.DialogResult 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定對話框結果值,這是從 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)
屬性值
Boolean類型的 Nullable<T> 值。 預設值為 false。
- 屬性
例外狀況
範例
下列範例示範如何設定 [確定] 按鈕和 [取消] 按鈕,以傳回適當的 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,以判斷使用者是否已接受或取消對話框(truefalse)。 如果已接受對話框,這表示開啟對話框的程序代碼,以擷取使用者收集的數據並加以處理。 不過,如果對話框已取消,這表示呼叫程式代碼應該停止任何進一步處理。
當使用者執行下列其中一項動作時,預設會取消對話框:
按 ALT+F4。
按兩下 [關閉] 按鈕
。 從 [系統] 選單選取 [關閉]
。
在這些情況下,預設會 falseDialogResult。
對話框通常會提供特殊按鈕來取消對話方塊,也就是 IsCancel 屬性設定為 true的按鈕。 以這種方式設定的按鈕會在按下視窗或按下 ESC 鍵時自動關閉視窗。 在上述任一情況下,DialogResult 會維持 false。
對話框通常也會提供接受按鈕,也就是 IsDefault 屬性設定為 true的按鈕。 以這種方式設定的按鈕會在按下 ENTER 鍵或 ENTER 鍵時引發其 Click 事件。 不過,它不會自動關閉對話框,也不會將 DialogResult 設定為 true。 您必須手動撰寫此程式碼,通常是從預設按鈕的 Click 事件處理程式。
當對話框顯示但未接受或取消時,DialogResult 會 null。
對話框關閉之後,您可以從 ShowDialog 方法傳回的值,或檢查 DialogResult 屬性,取得對話框結果。
DialogResult 只有在藉由呼叫 ShowDialog 方法開啟 Window 時,才能設定。
注意
當視窗載入在瀏覽器中時,您無法設定或取得這個屬性。