Reputation: 2315
What is the difference between sender and source in wpf event handling?
For example, say I had an ellipse in a canvas, and clicked on the ellipse: the ellipse would be both the sender and the source.
However if the ellipse doesn't handle the event but the main window does, the event will pass through the canvas... so the canvas would be the sender of the event to the main window but the source would be the ellipse.
Do I have that right?
Upvotes: 18
Views: 10497
Reputation: 93
Sender: Current element that is handling the event
OriginalSource: original object that first raised the event
Source: object that raised event. This is usually the same as OriginalSource but when dealing with Composite Controls it can be the parent that contains the OriginalSource object.*
RoutedEvent: Provides the RoutedEvent object for the event triggered by your event handler (such as the static UIElement.MouseUpEvent object). This information is useful if you’re handling different events with the same event handler.
Handled: Allows you to halt the event bubbling or tunneling process. When a control sets the Handled property to true, the event doesn’t travel any further and isn’t raised for any other elements.
Upvotes: 5
Reputation: 429
Hope this helps :)
*Common cases where the source may be adjusted include content elements inside a content model for a control (the contents of a list item, for instance, will report the list item element as the Source and the actual element within the list item will be the OriginalSource).
References:
Upvotes: 4
Reputation: 2513
Bubbles!
The sender is the object that the event is being raised from, whereas source was the original element that is causing the event to be raised.
Like in this case:
<TabControl Name="tc1" SelectionChanged="tc1_SelectionChanged">
<TabItem Header="1">
<TabControl Name="tc2">
<TabItem Header="1" />
<TabItem Header="2" />
</TabControl>
</TabItem>
<TabItem Header="2" />
</TabControl>
private void tc1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
In this case, if you change the SelectedItem
on the Sub-TabControl
, sender
will equal tc1
, Source
will equal tc2
.
Upvotes: 9
Reputation: 64078
The difference between the two is not often seen, as usually the sender
and Source
are the same. Most code written like Windows Forms will basically ignore the difference and send them along as the same reference. However, given how WPF's event routing works they represent two different concepts.
sender
is the object at which the event handler was attached. This is the owner that raised the handler to begin routing the event. From MSDN:
A difference between sender and Source is the result of the event being routed to different elements, during the traversal of the routed event through an element tree.
Source
is the object where the event originates. In the case of tunneling and bubbling, the Source
will be one of their child elements. You can use the OriginalSource
property to peel back any event tree encapsulation.
Upvotes: 25