分享

wpf

 牛人的尾巴 2015-12-01

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?

asked Aug 29 '11 at 21:05
mihajlv
1,0251434

4 Answers

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.

answered Aug 29 '11 at 21:32
user7116
44.4k1190134

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多