RoutedEventArgs.Source vs Sender

前端 未结 4 1056
执念已碎
执念已碎 2020-12-05 11:44

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:

相关标签:
4条回答
  • 2020-12-05 12:19

    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.

    MSDN: Event routing diagram

    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.

    0 讨论(0)
  • 2020-12-05 12:38

    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.

    0 讨论(0)
  • 2020-12-05 12:39

    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.

    0 讨论(0)
  • 2020-12-05 12:40

    Hope this helps :)

    • RoutedEventArgs.OriginalSource - original object that first raised the event
    • RoutedEventArgs.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.*
    • Sender - Current element that is handling the event

    *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:

    • http://msdn.microsoft.com/en-us/library/system.windows.routedeventargs.originalsource.aspx
    • http://msdn.microsoft.com/en-us/library/system.windows.routedeventargs.source.aspx (Includes mention of Sender)
    0 讨论(0)
提交回复
热议问题