How to pass a sender through an event handler

I am using the http://wpfmdi.codeplex.com/ library to handle MDI in my WPF application.

I have a Canvas that contains a child container, which in turn contains several small windows. I would like to perform an action when one of the small windows is closed, so I tried to do the following:

MdiChild child = new MdiChild(); child.Closing += new RoutedEventHandler(DatabaseTableWindow_Closing); private void DatabaseTableWindow_Closing(object sender, RoutedEventArgs e) { object s = e.Source; } 

As long as the method is successfully entered when the window is closed, e.Source is null. I also checked sender and it is empty too. All I want is a way to find out which window triggered the event.

0
source share
2 answers

If sender is null , it sounds like an oversight / error in your MDI environment. Since you have a source, you can fix it: find the place (s) where the Closing event will be generated, and add this as the sender. This should give you a reference to MdiChild when you are handling the event.

+2
source

Perhaps you can use LINQ to work around the problem:

 child.Closing += (o,e) => { DatabaseTableWindow_Closing(this, e); }; 

Change Actually in this case you should not use "this", but "child" (which points to your MdiChild):

 MdiChild child = new MdiChild(); child.Closing += (o,e) => { DatabaseTableWindow_Closing(child, e); }; 
+1
source

Source: https://habr.com/ru/post/1440972/


All Articles