首页 > 代码库 > 【2016-11-6】【坚持学习】【Day21】【子窗口关闭时,同步关闭它的主窗口】

【2016-11-6】【坚持学习】【Day21】【子窗口关闭时,同步关闭它的主窗口】

SubWindow 子窗口代码

 public partial class SubWindow : Window
    {

        public delegate void UpdateDelegate();
        public UpdateDelegate myHandle;
        public SubWindow()
        {
            InitializeComponent();

            this.Closed += SubWindow_Closed;
        }

        private void SubWindow_Closed(object sender, EventArgs e)
        {
            myHandle();
        }
    }

主窗口代码

 public partial class MainWindow : Window
    {
      
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            SubWindow s = new SubWindow();
            s.myHandle += Method1;
            s.Show();
        }

        private void Method1()
        {
            MessageBox.Show("子窗口要关闭啦。我们也要同步关闭了。");
            this.Close();
        }
    }

 

【2016-11-6】【坚持学习】【Day21】【子窗口关闭时,同步关闭它的主窗口】