user3227070 user3227070 - 3 months ago 15
C# Question

Wpf detect when window is closed from a different window

private void newmail_Click(object sender, RoutedEventArgs e)
{
Nieuweemail _nieuweEmail = new Nieuweemail(_username);
_nieuweEmail.Show();
}


When that window is closed, I want to call a function inside my main window that will execute.

_nieuweEmail.Closed += setContent();


I could do this if I could call it in the window that is going to be closed. But that's not the case. How can I detect this?

Answer

Assuming setContent and newmail_Click are both methods in your main window...

private void newmail_Click(object sender, RoutedEventArgs e)
{
    Nieuweemail _nieuweEmail = new Nieuweemail(_username);
    _nieuweEmail.Closed += SetContentHandler;
    _nieuweEmail.Show();
}

private void SetContentHandler(object sender, EventArgs e)
{
    setContent();
}