简体   繁体   中英

How to pass sender through event handler

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

I've got a Canvas which contains a child container, which in turn contains a number of 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;
        }

While the method is successfully entered when a window is closed, e.Source is null. I've also checked the sender and that is null too. All I want is a way to find out which window fired the event.

If the sender is null , then it sounds like an oversight/bug in the MDI framework you are using. Since you have the source, you can fix it: locate the place(s) where the Closing event is raised, and add this as the sender. That should give you a reference to the MdiChild when you are handling the event.

You can probably use LINQ to circumvent the issue:

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

Edit : Actually in this case you should not use "this", but "child" (which would point to your MdiChild):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM