.Net Advanced concepts

Exploring .Net Technology

WPF Routing

WPF (3.5) introduced the concept of Routing that made the event routing easies in the scenarios where it was tedious to handle events. Consider a scenario where there are a number of Hyperlinks in a Panel that direct to separate locations on Click. Now if this is done in normal programming, each hyperlink will have to have code for execution. It would be easier and cleaner if we could handle the hyperlinks in the container (the Panel) that handles the click and redirects to appropriate location.

WPF handles the events with the following 3 strategies.

  • Direct events are like ordinary .NET events. They originate in one element and don’t pass to any other. For example, MouseEnter is a direct event.
  • Bubbling events are events that travel up the containment hierarchy. For example, MouseDown is a bubbling event. It is raised first by the element that is clicked. Next, it is raised by that element’s parent, and then by that element’s parent, and so on, until WPF reaches the top of the element tree.
  • Tunneling events are events that travel down the containment hierarchy. They give you the chance to preview (and possible stop) an event before it reaches the appropriate control. For example, PreviewKeyDown allows you to intercept a key press, first at the window level, and then in increasingly more specific containers until you reach the element that had focus when the key was pressed.

RoutedEventArgs Class:

  • Source indicates what object raised the event.
  • OriginalSource indicates what object originally raised the event. Usually the OriginalSource is the same the Source. But in some cases they could be different. For example, if you click close to the border of a window, you will get a Window object for the Source but a Border object for the OriginalSource.
  • RoutedEvent provides the RoutedEvent object for the event triggered by your event handler.
  • Handled allows you to halt the event bubbling or tunneling process.

Here is a simple example of when you might use a bubbling event:

<StackPanel Hyperlink.Click=”StackPanel_Click”>

        <TextBlock>

        <Hyperlink NavigateUri=”http://www.google.com”>Google</Hyperlink&gt;

        </TextBlock>

        <TextBlock>

       <Hyperlink NavigateUri=http://gmail.com>Gmail</Hyperlink>

        </TextBlock>

        <TextBlock>

       <Hyperlink NavigateUri=http://facebook.com>FaceBook</Hyperlink>

        </TextBlock>

    </StackPanel>

And to handle the HyperLinks..

private void StackPanel_Click(object sender, RoutedEventArgs e)

        {

            Process.Start(((Hyperlink)e.Source).NavigateUri.ToString());

        }

So instead of having to create an event handler for each hyperlink, or creating a single event handler and pointing each hyperlink to the same event handler, one event handler on the parent element can be used.

This article just covered the Routing Basics and can serve as a base material to get started, However there is still lot that needs to be learnt and the who else, other than MSDN can teach it better.

Hope this was helpful,

Till Next we Connect…….

Happy Learning.

The Source Code can be downloaded from here

January 11, 2011 - Posted by | WPF

1 Comment »

  1. […] Routed Events has been covered in this post […]

    Pingback by WPF Overview-Part-III « .Net Advanced concepts | January 17, 2011 | Reply


Leave a comment