Reputation: 145
I have a short questions and even after a while of searching through the web I have not found the answer. I have two windows in a WPF application. One window should just be hidden when the user closes it. When the main window is closed the complete application shall close.
I used
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
Hide();
}
inside the class of the second window and hoped it would just catch its Close() event but unfortunately it catches all Close() events.
How can I separate between the windows to handle the events independently?
Regards Larimow
Upvotes: 0
Views: 189
Reputation: 437326
Use the sender
parameter, that's what it's there for:
window1.Closing += Window_Closing;
window2.Closing += Window_Closing;
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
var w = (Window)sender;
// Simple ref test to illustrate, but you can use anything else you want instead
if (w == window1) {
e.Cancel = true;
w.Hide();
}
}
Upvotes: 1