Dot NET
Dot NET

Reputation: 4897

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.

Upvotes: 0

Views: 1216

Answers (2)

Joe
Joe

Reputation: 2564

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); };

Upvotes: 2

Jay
Jay

Reputation: 57939

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.

Upvotes: 2

Related Questions