RoutedEventArgs.Source与发件人 [英] RoutedEventArgs.Source vs Sender

查看:104
本文介绍了RoutedEventArgs.Source与发件人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在wpf事件处理中发件人之间有什么区别?



例如,说我在画布上有一个椭圆,并点击了椭圆:椭圆将是发件人



但是,如果椭圆不处理主窗口,事件将通过画布...,所以画布将是主窗口的事件的发件人但是将是椭圆。



我有这个权利吗?

解决方案

两者之间的区别并不常见,通常发件人来源是一样的大多数写成Windows窗体的代码基本上将忽略差异,并将它们作为相同的引用发送。然而,鉴于WPF的事件路由工作原理,它们代表了两个不同的概念。



在隧道和冒泡,将是他们的子元素之一。您可以使用 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与发件人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆