Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
629 views
in Technique[技术] by (71.8m points)

wpf - RoutedEventArgs.Source vs Sender

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...