收藏 Silverlight中子窗体关闭刷新父窗体(转载)


    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            LoginWindow loginWnd = new LoginWindow();
            loginWnd.Closed += new EventHandler(loginWnd_Closed);
            loginWnd.Show();
        }
        void loginWnd_Closed(object sender, EventArgs e)
        {
            LoginWindow lw = (LoginWindow)sender;

            if (lw.DialogResult == true && lw.nameBox.Text != string.Empty)
            {
                this.helloTxt.Text = "Hello " + lw.nameBox.Text;
            }
            else if (lw.DialogResult == false)
            {
                this.helloTxt.Text = "Login canceled.";
            }
        }
    }

    public partial class LoginWindow : ChildWindow
    {
        public LoginWindow()
        {
            InitializeComponent();
        }
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        private void ChildWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (this.DialogResult == true &&
                    (this.nameBox.Text == string.Empty || this.passwordBox.Password == string.Empty))
            {
                e.Cancel = true;
                ChildWindow cw = new ChildWindow();
                cw.Content = "Please enter name and password or click Cancel.";
                cw.Show();
            }
        }
    }
posted @ 2013-08-29 16:56  sandea  阅读(222)  评论(0编辑  收藏  举报