【2016-11-6】【堅持學習】【Day21】【子窗口關閉時,同步關閉它的主窗口】

SubWindow 子窗口代碼this

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

主窗口代碼spa

 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();
        }
    }
相關文章
相關標籤/搜索