How to catch the event of the window close button(red X button on window right top corner) in wpf form?

前端 未结 6 1285
醉酒成梦
醉酒成梦 2021-01-03 18:22

How can I catch the event of the window close button(red X button on window right top corner) in a WPF form? We have got the closing event, window unloaded event also, but w

6条回答
  •  独厮守ぢ
    2021-01-03 19:07

    if it is pressed confirm button in form2 do action, if it is pressed X-button do nothing:

    public class Form2
    {
      public bool confirm { get; set; }
    
        public Form2()
            {
                confirm = false;
                InitializeComponent(); 
            }
    
       private void Confirm_Button_Click(object sender, RoutedEventArgs e)
        {
           //your code
           confirm = true;
           this.Close();
    
        }
    
    }
    

    first form:

    public void Form2_Closing(object sender, CancelEventArgs e)
            {
                if(Form2.confirm == false) return;
    
                //your code 
            }
    

提交回复
热议问题