Routed Events in WPF

A routed event is a type of event that can invoke handlers on multiple listeners in an element tree, rather than just on the object that raised the event. These events which navigate up or down the visual tree according to their RoutingStrategy. The routing strategy can be bubble, tunnel or direct. The concept of Routed Events comes into the picture when we want to handle an event, that is originated from some other control in the hierarchy. Say for example if any user clicks the Button control, that event which is normally a Button_click event can be raised by The Button, The Label, The StackPanel or The Window.

A typical WPF application contains many elements. Whether created in code or declared in XAML, these elements exist in an element tree relationship to each other. The event route can travel in one of two directions depending on the event definition, but generally, the route travels from the source element and then \”bubbles\” upward through the element tree until it reaches the element tree root (typically a page or a window).

<Border Height=\"50\" Width=\"300\" BorderBrush=\"Gray\" BorderThickness=\"1\">
    <StackPanel Background=\"LightGray\" Orientation=\"Horizontal\" Button.Click=\"CommonClickHandler\">
      <Button Name=\"YesButton\" Width=\"Auto\" >Yes</Button>
      <Button Name=\"NoButton\" Width=\"Auto\" >No</Button>
      <Button Name=\"CancelButton\" Width=\"Auto\" >Cancel</Button>
    </StackPanel>
  </Border>

In this simplified element tree, the source of a Click event is one of the Button elements, and whichever Button was clicked is the first element that has the opportunity to handle the event. But if no handler attached to the Button acts on the event, then the event will bubble upwards to the Button parent in the element tree, which is the StackPanel. Potentially, the event bubbles to Border, and then beyond to the page root of the element tree. In other words, the event route for this Click event is:

Button–>StackPanel–>Border–>???

Routing Strategies

Routed events use one of three routing strategies:

Tunneling: The event is raised on the root element and navigates down to the visual tree until it reaches the source element or until the tunneling is stopped by marking the event as handled. By naming convention it is called Preview??? and appears before corresponding bubbling event.

Bubbling: The event is raised on the source element and navigates up to the visual tree until it reaches the root element or until the bubbling is stopped by marking the event as handled. The bubbling event is raised after the tunneling event.

Direct: The event is raised on the source element and must be handled on the source element itself. This behavior is the same as normal .NET events.

\"\"/

How to Create a Custom Routed Event

public static readonly RoutedEvent TapEvent = EventManager.RegisterRoutedEvent(
      \"Tap\", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyButtonSimple));
 
  // Provide CLR accessors for the event 
  public event RoutedEventHandler Tap
  {
          add { AddHandler(TapEvent, value); } 
          remove { RemoveHandler(TapEvent, value); }
  }

A routed event is a CLR event that is backed by an instance of the RoutedEvent class and registered with the WPF event system. The RoutedEvent instance obtained from registration is typically retained as a public static read-only field member of the class that registers and thus \”owns\” the routed event. The connection to the identically named CLR event (which is sometimes termed the \”wrapper\” event) is accomplished by overriding the add and remove implementations for the CLR event. Ordinarily, the add and remove are left as an implicit default that uses the appropriate language-specific event syntax for adding and removing handlers of that event. The routed event backing and connection mechanism is conceptually similar to how a dependency property is a CLR property that is backed by the DependencyProperty class and registered with the WPF property system.

/Siva

Leave a Reply

Scroll to Top
%d bloggers like this: