wpf 冒泡和隧道事件之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16736444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between Bubbling and Tunneling events
提问by Neelendu
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!
冒泡事件和隧道事件之间的确切区别是什么?我应该在哪里使用冒泡事件以及我应该在哪里使用隧道事件?提前致谢!
回答by Joetjah
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
MouseButtonDownevent. Or aKeydownevent.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
previewdownkeyandpreviewmousebuttondown. 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.
WPF 为我们提供了许多不同的事件处理机制——它们是冒泡、隧道和直接。这些都称为路由事件。
直接事件
您可能已经习惯了直接路由事件。这是项目本身处理发生的事件的地方。一个很好的例子是
onClick在标准 WinForms 中处理鼠标按钮的he事件。这是在 GUI 项中引发事件并由所述 GUI 元素处理的地方。冒泡事件
现在我们都喜欢某种形式的泡沫。当事件没有被元素处理(比如 a
textbox)并且事件“冒泡”到包含它的 UI 容器时,就会发生冒泡。例如,假设您有一个包含面板的窗口,在该面板内有一个网格,在网格内有一个文本框。如果文本框未处理该事件,则它会移动、传递或“冒泡”到网格级别(因为网格包含文本框),如果未在该级别处理,则事件会进一步向上冒泡“树”(称为可视化树)到面板,在那里它可能会或可能不会被处理。这个过程一直持续到它被处理或事件“逃脱”最顶层的元素。冒泡事件的示例类似于
MouseButtonDown事件。或者一个Keydown事件。隧道
隧道与冒泡相反。因此,事件不是沿着可视化树“向上”移动,而是沿着可视化树向下传播到被视为源的元素。隧道事件的标准 WPF 命名定义是它们都以“预览”开头,例如
previewdownkey和previewmousebuttondown. 您可以在它们到达“目标”元素的途中捕捉它们并进行处理。这方面的一个例子可能是,您可能在网格控件中有一些控件,并且出于某种原因,您决定不允许该网格内的任何控件让字母“t”到达它。
Sourcewith the opinion of the author which I don't support nor agree with.
来源与作者的意见,我不支持也不同意。
And another StackOverflow question which is pretty much the same.
And last but not least some explanation and another tutorial.
回答by Bas
As a start: the naming convention in WPF for some default events is Preview<event>for tunneling and <event>for bubbling. So for example for the KeyDown we would have PreviewKeyDownand KeyDown, tunneling and bubbling respectively.
首先:WPF 中某些默认事件的命名约定是Preview<event>用于隧道和<event>冒泡。因此,例如,对于 KeyDown,我们将分别有PreviewKeyDown和KeyDown、隧道和冒泡。
The difference between the two, as the naming convention implies, is that a tunneling event will start at the highest node in the tree (probably the Window) and going down to the lowest child. A bubbling event will start at the child and then go upwards again.
正如命名约定所暗示的那样,两者之间的区别在于隧道事件将从树中的最高节点(可能是窗口)开始,并向下到达最低的子节点。冒泡事件将从孩子开始,然后再次向上。
This guide should explain it clearly: http://www.codeproject.com/Articles/464926/To-bubble-or-tunnel-basic-WPF-events
本指南应该清楚地解释它:http: //www.codeproject.com/Articles/464926/To-bubble-or-tunnel-basic-WPF-events

