天天看點

WPF 用ShowDialog的方法顯示視窗的傳回值

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;
        }
    }
}
      

隻要設定了DialogResult屬性(true或false),視窗就會退出,ShowDialog()的傳回值就是DialogResult,然後通過判斷DialogResult是true還是false來确定使用者意願。