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

前端 未结 6 1283
醉酒成梦
醉酒成梦 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:14

    at form1.Designer.cs put below code to assign the event

    this.Closing += Window_Closing;
    

    at form1.cs put the closing function

    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        //change the event to avoid close form
        e.Cancel = true;
    }
    

提交回复
热议问题