Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Tunneling, practical use?

What practical scenarios need to use Tunneling of events?

I know a vague answer to this could be that it is used when we want to handle a event across the visual/logical on which event is raised. But that's just theory.

In practice why should I tunnel an event?

Also does tunneling (and bubbling) works across Logical tree or Visual tree or both?

like image 539
WPF-it Avatar asked Jul 08 '11 09:07

WPF-it


People also ask

What is tunneling in WPF?

Tunnel Event In a WPF application, events are often implemented as a tunneling/bubbling pair. So, you'll have a preview MouseDown and then a MouseDown event. Given below is a simple example of a Routed event in which a button and three text blocks are created with some properties and events.

What is bubbling and tunneling in XAML?

Bubbling is a Bottom-Up approach. (i.e. From control which fires the event to the topmost control in the live-visual tree.) For example, from button to Window/UserControl. Where tunneling is a Top-Down approach. i.e. From the topmost control to the control which fires the event.

What is routing in WPF?

A routed event is an event registered with the WPF event system, backed by an instance of the RoutedEvent class, and processed by the WPF event system. The RoutedEvent instance, obtained from registration, is typically stored as a public static readonly member of the class that registered it.


4 Answers

I find tunneling events to be very useful for handling events on elements which I have not created myself, and hence have no easy way to add an event handler to. For example, the ItemsControl generates UI elements based on a template, as a result it is not always a straightforward process to get a reference to these elements. Rather than adding an event handler to each item which the ItemsControl generates you can instead handle the tunnelled event on a single parent element.

Routed events traverse a 'hybrid' tree, which is neither the visual or logical tree. This sounds a bit crazy, but it has never cause me problems in the past.

like image 159
ColinE Avatar answered Oct 06 '22 21:10

ColinE


Tunnel events are used in WPF for all the OnPreview events. Tunneling makes sense if you want the actual targeted element to be the last that receives the event.

If you have a list box with an item which contains a textbox. Clicking the textbox will consume the click thus not selecting the list box. If you use the tunneled or preview mouse click event, you can select first, leaving it unhandled and then it travels to the text box. Also note that in WPF every event will first fire a preview then a bubble event, if the preview event was unhandled.

Msdn:

Tunneling: Initially, event handlers at the element tree root are invoked. The routed event then travels a route through successive child elements along the route, towards the node element that is the routed event source (the element that raised the routed event). Tunneling routed events are often used or handled as part of the compositing for a control, such that events from composite parts can be deliberately suppressed or replaced by events that are specific to the complete control. Input events provided in WPF often come implemented as a tunneling/bubbling pair. Tunneling events are also sometimes referred to as Preview events, because of a naming convention that is used for the pairs.

AFAIK the Visual tree is used for traversing, but because MSDN is down i can't look for a proper source.

like image 29
dowhilefor Avatar answered Oct 06 '22 21:10

dowhilefor


This is a quote from MSDN:

Tunneling: Initially, event handlers at the element tree root are invoked. The routed event then travels a route through successive child elements along the route, towards the node element that is the routed event source (the element that raised the routed event). Tunneling routed events are often used or handled as part of the compositing for a control, such that events from composite parts can be deliberately suppressed or replaced by events that are specific to the complete control. Input events provided in WPF often come implemented as a tunneling/bubbling pair. Tunneling events are also sometimes referred to as Preview events, because of a naming convention that is used for the pairs.

Tunneling events are what is commonly known as Preview-events.

enter image description here

You might also want to read this MSDN Article on Advanced WPF: Understand Routed Events and Commands in WPF. @Robert Havery wrote a good explenation to a similar question here on SO.

like image 37
Filip Ekberg Avatar answered Oct 06 '22 22:10

Filip Ekberg


User interactions are often represented by pairs of events - one that previews the interaction (tunneling) and one that reacts to it (bubbling). Having the tunneling "preview" event first allows the parent control to intercept the interaction.

As an example, if you were to create your own button style user control a part of the control might be an icon. If the user were to click the icon you would intercept the tunneling click preview event because the icon doesn't need to know it was clicked. You could then raise a bubbling event informing any controls containing your button that it had been clicked.

like image 44
Phil Gan Avatar answered Oct 06 '22 22:10

Phil Gan