冒泡和隧道事件之间的区别 [英] Difference between Bubbling and Tunneling events

查看:152
本文介绍了冒泡和隧道事件之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

冒泡事件和隧道事件之间的确切区别是什么?
我应该在哪里使用冒泡事件,我应该在哪里使用隧道事件?
感谢提前!

解决方案


WPF为我们提供了许多不同的处理机制事件 -
他们冒泡,隧道和直接。这些都被称为
路由事件。



直接事件



您可能已经习惯了直接路由事件。这是
,其中项目本身处理发生的事件。一个很好的例子
将处理他在标准
WinForms中的 onClick - 鼠标按钮的事件。这是在GUI项目中引发事件的地方,并获取由所述GUI元素处理的



冒泡事件



现在我们都喜欢一种形式的气泡。当事件不由元素处理(例如一个文本框)和
事件气泡时,冒泡发生在
之上,UI容器拿住。对于
示例,假设您有一个包含面板的窗口,并且
中的面板您有一个网格,并且在网格内有一个文本框。如果
事件不由文本框处理,则它移动,被传递或
气泡直到网格级别(如网格包含文本框),如果
它不是处理在该级别,然后事件进一步向
树(称为视觉树)向面板发送,可能或可能不
被处理。这个过程一直持续到它被处理或事件
转义最顶层的元素。



冒泡事件的例子将是一个
MouseButtonDown 事件。或 Keydown 事件。



隧道



隧道与Bubbling相反。因此,事件不是像
up的视觉树,而是从视觉树向
的元素传递给被认为是源的元素。标准WPF命名
定义的隧道事件是他们都以预览
开头,例如 previewdownkey previewmousebuttondown 。你可以
抓住他们的路径到目标元素并处理它。一个
的例子可能是你在网格
控制中有一些控件,由于某些原因,您决定在
内没有控件允许网格具有字母t达到它。


,作者的意见,我不支持或同意。



另一个StackOverflow问题几乎是一样。



一个很好的演示项目



最后但并非最不重要的一些解释和另一个教程。


What is the exact difference between Bubbling Events and Tunneling events? Where should I use Bubbling Events and where should I use Tunneling events? Thanks in Advance!

解决方案

WPF gives us a number of different mechanisms for handling events – they are bubbling, tunneling, and direct. These are all known as Routed events.

Direct event

You are probably already used to the direct routed event. This is where the item itself handles the event that occurred. A good example would be handling he onClick-event of a mouse button in standard WinForms. This is where the event is raised in the GUI item and gets handled by said GUI element.

Bubbling Event

Now we all like some bubbles in one form or another. Bubbling happens when the event is not handled by the element ( say a textbox) and the event "bubbles" its way up the UI containers which hold it. For example, let's say you have a window that contains a panel and inside that panel you have a grid and inside the grid you have a textbox. If the event is not handled by the textbox, then it moves, is passed or "bubbles" up to the grid level (as the grid contains the textbox), if it is not handled at that level then the event bubbles further up the "tree" (known as a visual tree) to the panel where it may or may not be handled. This process continues until it is handled or the event "escapes" the top most element.

Examples of a bubbling event would be something like a MouseButtonDown event. Or a Keydown event.

Tunneling

Tunneling is the opposite of Bubbling. So instead of an event going "up" the visual tree, the event travels down the visual tree toward the element that is considered the source. The standard WPF naming definition of a tunneling event is that they all start with "preview" for example previewdownkey and previewmousebuttondown. You can catch them on their way to the "target" element and handle it. An example for this might be perhaps you have some controls inside a grid control and for some reason you have decided that no control within that grid will be allowed to have the letter "t" reach it.

Source with the opinion of the author which I don't support nor agree with.

And another StackOverflow question which is pretty much the same.

A nice demo project.

And last but not least some explanation and another tutorial.

这篇关于冒泡和隧道事件之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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