RoutedEventArgs.Source 与 Sender [英] RoutedEventArgs.Source vs Sender
问题描述
sender 和 source 在 wpf 事件处理上有什么区别?
例如,假设我在画布中有一个椭圆,然后单击该椭圆:该椭圆既是 发送者,又是 源.
然而,如果椭圆不处理事件而主窗口处理,事件将通过画布...所以画布将成为主窗口事件的发送者但源将是椭圆.
我有这个权利吗?
两者的区别并不常见,通常sender
和Source
是一样的.大多数像 Windows 窗体一样编写的代码基本上会忽略差异并将它们作为相同的参考发送.但是,考虑到 WPF 的事件路由的工作方式,它们代表了两个不同的概念.
在隧道和冒泡的情况下,Source
将是它们的子元素之一.您可以使用 OriginalSource
属性剥离任何事件树封装.
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?
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.
这篇关于RoutedEventArgs.Source 与 Sender的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!