Thursday, July 21, 2011

WPF Routed Event

Routed Events


Functional definition: 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.

Implementation definition: A routed event is a CLR event that is backed by an instance of the RoutedEvent class and is processed by the Windows Presentation Foundation (WPF) event system.
A particular event feature that is fundamental to WPF is a routed event. Routed events enable an element to handle an event that was raised by a different element, as long as the elements are connected through a tree relationship. When specifying event handling with a XAML attribute, the routed event can be listened for and handled on any element, including elements that do not list that particular event in the class members table. This is accomplished by qualifying the event name attribute with the owning class name. For instance, the parent StackPanel containing Button could register a handler for the child element button's Click event by specifying the attribute Button.Click on the StackPanel object element, with your handler name as the attribute value.

See for e.g. the XAML below contains the StackPanel as a parent element and in the hierarchy contains the button element thus forms a VisualTree (StackPanel ---- Button):
 <StackPanel>  <Button Content="Click Me" Click="btn_Clicked"/> </StackPanel>
void btn_Clicked (object sender, RoutedEventArgs args)
{  //logic to handle the Click event }
  
Common handler for all the buttons contained within the StackPanel:

  <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>
     void CommonClickHandler (object sender, RoutedEventArgs args)
    {
  //logic to handle the Click event  FrameworkElement feSource = e.Source as FrameworkElement;
  switch (feSource.Name)
  {    case "YesButton":
      // do something here ...      break;
    case "NoButton":
      // do something ...      break;
    case "CancelButton":
      // do something ...      break;
  }  e.Handled=true; } 

In WPF, there are three types of event notification methods, namely:
Bubbling: Allows events to bubble up the VisualTree (the tree of Visual UI Elements), to the root element.

Tunneling: Allows events to tunnel down the VisualTree.

Direct: Works just like the old .NET 1.x/2.0 CLR events. Only the subscriber gets to see the event.

Figure 1: VisualTree showing Bubbling and Tunneling



What actually happens is that when an event is raised, it travels up and down the VisualTree, invoking any handlers that are subscribed to the RoutedEvent. This traversal of the VisualTree will not be the entire tree, but rather the portion of the tree that is directly related to the element that raised the event.

Naming Conventions
 There is a naming convention in place to aid in determining how these events were created.
Tunneling events usually start with "PreviewXXX" and Bubbling events are usually just XXX.
 For example, PreviewKeyDown (Tunneling) and KeyDown (Bubbling).




References: 
For more details see MSDN source. Also see Post By Sacha Barber.


No comments:

Post a Comment