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

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

问题描述

冒泡事件和隧道事件之间的确切区别是什么?

What is the exact difference between Bubbling Events and Tunneling events?

我应该在哪里使用冒泡事件以及应该在哪里使用隧道事件?

Where should I use Bubbling Events and where should I use Tunneling events?

推荐答案

WPF 为我们提供了许多不同的事件处理机制——它们是冒泡的、隧道的和直接的.这些都被称为路由事件.

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

直接事件

您可能已经习惯了直接路由事件.这是项目本身处理发生的事件的位置.一个很好的例子将在标准中处理鼠标按钮的 onClick-事件赢表格.这是在 GUI 项中引发事件并获取的位置由所述 GUI 元素处理.

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.

冒泡事件

现在我们都喜欢某种形式的气泡.冒泡发生当事件未被元素处理时(比如 textbox)和事件在容纳它的 UI 容器中冒泡".为了例如,假设您有一个包含面板和内部的窗口该面板有一个网格,在网格内有一个文本框.如果该事件不由文本框处理,然后它移动,传递或冒泡"到网格级别(因为网格包含文本框),如果它没有在该级别处理,然后事件会进一步向上冒泡树"(称为视觉树)到可能或可能不存在的面板被处理.这个过程一直持续到它被处理或事件转义"最顶层的元素.

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.

冒泡事件的示例类似于MouseButtonDown 事件.或 Keydown 事件.

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

隧道

隧道与冒泡相反.所以不是一个事件发生向上"视觉树,事件沿着视觉树向下传播被认为是源的元素.标准的 WPF 命名隧道事件的定义是它们都以预览"开头例如 previewdownkeypreviewmousebuttondown.你可以在他们前往目标"元素的路上抓住他们并处理它.一个例如,您可能在网格中有一些控件控制,由于某种原因,您已决定内部没有控制该网格将被允许让字母t"到达它.

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.

还有一个很漂亮的 StackOverflow 问题大同小异.

一个不错的演示项目

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

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

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