Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if Window Close was completed or cancelled?

Tags:

c#

wpf

WPF Application. Window1 can open WindowA.Show(), WindowB.Show(), WindowC.Show()

When we close Window1 we want to close all open windows as well (A, B and C).

On Window1_Closing event we call

WindowA.Close();
WindowB.Close();
WindowC.Close();

On window closing of any of these Cancel = true can be called and the WindowA (or B or C) won't close. Then we don't want to close Window1 (the parent). How to know in Window1_Closing if any of the child windows was cancelled (not closed)?

like image 758
st_stefanov Avatar asked Jan 21 '26 08:01

st_stefanov


1 Answers

  1. Window.Closed event is fired after the window is closed. Either move your logic to eventhandler of Closed event, or use the event to send some flag:

    bool isClosed = false;
    WindowA.Closed += delegate { isClosed = true; };
    WindowA.Close();
    
    if (isClosed) { 
    }
    
  2. Application.Current.Windows is collection of currently opened windows:

    WindowA.Close();
    bool isClosed = !Application.Current.Windows.OfType<Window>().Contains(WindowA);
    
like image 178
Liero Avatar answered Jan 23 '26 23:01

Liero