Understanding Event Handling In WPF – Direct, Tunneling, Bubble Event Handling

Last wpf tutorial was on how to implement events in wpf. We learned to implement the wpf events in two ways i.e. Declarative approach and Managed code approach. In response I got so many emails related to event handling in WPF. What i realized from the emails is that most of the people don’t know or have a less knowledge on Event handling. So I decided to write up next article on basic understanding on WPF Event Handling.

What is Event handling?

An Event handling is a mechanism that lets you control the behaviour of the application when a specific event is occurred. Event is basically an action that your application detects like mouse movements or mouse click or pressing a keyboard button. Any Graphical User Interface (GUI) application is basically an event driven application. The three major components of Event handling are Events, Event Source and Event Listeners.

  1. Events: As described above, an event is a specific action or just a change of a object state.
  2. Event Source: Event source generates the events.
  3. Event Listeners: Event listeners listens the events. As soon as events occurs, listener gets notified.

Basic understanding of Event Handling in WPF

Event Handling in WPF
Event Handling in WPF

Windows Presentation Foundation (WPF) gives us three types of event handling mechanism.

  1. Direct Events
  2. Bubble Events
  3. Tunneling Events

These three events are also called as Routed Events. These routed events are introduced by WPF itself.

What is Routed Events?

The routed event is also called as CLR events. The traditional event invokes on only when the object raised the event. According to msdn, routed events can invoke handlers on multiple listeners in an element tree rather than just on the object that raised the event. It means depending upon the RoutingStragety the event actually navigates upwards or downwards the visual tree. In short the routed events are those events which navigates up or down in visual tree.

A typical WPF appliation always contains a visual tree of many elements. All these elements maintain their relationship to each other in this visual tree. As the relationship gets maintained in tree structure, while navigating the event route can travel only in two directions i.e. either top to bottom or bottom to top. The top elements would always be Root Element and rest of the other elements are considered as leaf elements.
Based on the travel direction of event route we get direct event, bubble event or tunnel event. The most important thing is that routed events normally comes in pair. For ex. PreviewMouseDown (Tunnel) as a first event and MouseDown (Bubble) as a second event.

1. Direct Events

We all already know about this routed event type. A normal .Net event behavior where the object raises and handles the event itself is called as Direct Event handling. MouseEnter is the best example of this type of event handling. MouseEnter fires only when a cursor pointer moves over the element.

2. Bubble Events

The event is raised at any source object element and then it travels up the visual tree and finally reaches to top of the visual tree.

Bubble Event Handling in WPF
Bubble Event Handling in WPF

Consider an event is raised at any child element then it reaches to its parent element, then reaches to parent of a parent element and goes on, until it reaches the top of the visual tree. MouseDown events are the examples of this event handling.

Another example could be a visual tree where a StackPanel has Button element inside. You clicked the button, then at first event get fires at Button control, then get fires on parent StackPanel and then the parent control of StackPanel.

3. Tunnel Event Handling

Tunnel event works just opposite as Bubble events. Bubble event travels in upward direction whereas tunnel event navigates to downward direction the visual tree. The root fires the event, then the event reaches to its child element and then reaches to the child element of a child and goes on. The navigation stops only when the event reaches at source control element or explicitly marked event as handled in between before it reaches to the source element.

Tunnel Event Handling in WPF
Tunnel Event Handling in WPF

This event handling strategy gives you the chance to preview the an event. PreviewKeyDown is an example of Tunneling event handling strategy. Whenever there is Preview in event names then it is always Tunnel event handling.

Some interesting points related to tunnel and bubble event handling in wpf

  1. Both of these events in wpf are usually comes in pair. Like PreviewMouseDown and MouseDown.
  2. The RoutedEventArgs instance always shared by these event handling strategies. That means if you marks the tunnel event as handled then it won’t occurs for bubble also.
  3. Routing can only stop when it reached to either top element or bottom element. To stop explicitly you will have to set
    e.Handled = true;

This wpf tutorial gives you an understanding of basic event handling in WPF. Based on the implementation this can be futher categorized as Declarative and Managed code events.

I hope you enjoyed the article. If you find this article helpful, then could you please share the article on your social media?

Following few articles might be of your interest –

About the Author: Ashish Ramteke

Ashish Ramteke is a Software Professional, Part time blogger and founder of C# Tutorials, a popular blog on C#.Net. Ashish is having 6 years of work experience and an engineering degree in Computer Science and Engg.

Leave a comment