Events in Windows Presentation Foundation
page 2 of 5
by Keyvan Nayyeri
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 42834/ 69

Routing Strategy

If you've worked with older graphic user interfaces, you are aware of a concept: in Windows Forms applications, only one control receives an event.  For example when you click a button, only this button receives the Click event.  But in Windows Presentation Foundation there is a key difference here: other elements can receive an event for an element.  For example you may click on a button but its preceding elements receive Click events as well.

Windows Presentation Foundation supports different types of routing strategies which are listed with a short description below.  Before stepping into these three types, it is worthwhile to have something in mind.  Imagine elements organized in a hierarchical manner.  That is, suppose that in a tree parent elements are in upper level and children are in lower level.

·         Bubble: A Bubble event starts from an element and follows for all its preceding elements.  When a Bubble event occurs, first the element itself receives the event then its parent, then the parent of parent and so on.

·         Direct: Direct events are similar to traditional Windows Forms events.  Only the element, itself, receives the event.

·         Tunnel: Tunnel events are in opposite direction of Bubble events.  It means when a Tunnel event occurs, first root element receives it, then its first child, then child of this child and so on until it arrives to element, itself.

In working with the above routing strategies you need to be aware of some points that I'll address in a moment:

·         As you see Bubble and Tunnel strategies are in opposite directions.  Here Microsoft has done a naming convention to help developers find their way easier.  Tunnel event names start with a "Preview" prefix.  So you can identify Tunnel events by their names.  For example MouseRightButtonDown is a Bubble event but PreviewMouseRightButtonDown is a Tunnel event.

·         It's recommended to mark events as Handled when you're sure there is no other element in the route.  This has a performance effect.  Handled is a property for event argument types.

All events in Windows Presentation Foundation belong to one of the above three types.  For example the Click event has a Direct routing strategy, MouseLeftButtonDown has a Bubble routing strategy and PreviewMouseLeftButtonDown has a Tunnel routing strategy.

Let's see an example of Bubble events.

Listing 1 and Listing 2 represent XAML and logic codes for a simple application which uses the MouseEnter event (with the Bubble strategy). 

Listing 1

<Window x:Class="EventsInWPF.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="EventsInWPF" Height="100" Width="250"
    >
    <Grid MouseEnter="GridHandler">
      <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
      </Grid.ColumnDefinitions>
      <Button MouseEnter="ButtonHandler" Grid.Row="0" Grid.Column="0"
              Width="100" Height="20">Click</Button>
    </Grid>  
</Window>

Listing 2

private void ButtonHandler(object sender, MouseEventArgs e)
{
    Debug.WriteLine("Event Received By Button");
}
 
private void GridHandler(object sender, MouseEventArgs e)
{
    Debug.WriteLine("Event Received By Grid");
}

The output of Listing 2 shows the order of events by providing the output when you move mouse over Button as shown below

Event Received By Button
Event Received By Grid

Clearly the event was received by both the Button and its parent, as expected.


View Entire Article

User Comments

Title: Easy to learn   
Name: Niranjan
Date: 11/29/2009 9:05:16 PM
Comment:
Thanks for the efforts. The article is simple to understand.Great efforts.
Title: Excellent   
Name: shaukat
Date: 6/29/2007 3:50:54 AM
Comment:
Excllent
Title: Thanks   
Name: Keyvan Nayyeri
Date: 1/15/2007 11:38:12 PM
Comment:
Thanks Sam,

But if I can remember correctly, MSDN Wiki wasn't available when I wrote the first part of these tutorials to add it to introduction article because it was before RC days.

However, it was better to leave this comment there but thank you for sharing it with others.
Title: Need link to WPF Online SDK part 2   
Name: Sam
Date: 1/15/2007 8:01:50 PM
Comment:
Oops, posting another comment 'cause the URL didn't render. Here it is: http://msdn2.microsoft.com/en-us/library/ms754130.aspx
Title: Need a link to online WPF SDK   
Name: Sam
Date: 1/15/2007 8:00:01 PM
Comment:
There are a ton of WPF articles that would help newbies and experienced alike. Please add a link to that page so people know where to go next.
Title: Thanxٍ   
Name: Behnam Usefi
Date: 12/30/2006 2:00:31 AM
Comment:
Thanx K1
Title: Thanks   
Name: Keyvan Nayyeri
Date: 12/14/2006 11:02:58 AM
Comment:
Thanks Mohammad,

I tried to cover fundamental concepts for those who only want to have a background but surely you need a book if you want to work professionally on WPF and XAML.

Thanks again :-)
Title: Another great article !!!   
Name: AzamSharp
Date: 12/14/2006 10:59:42 AM
Comment:
Another great article! Why buy a book when I can read this great series. :)

Thanks and plus thanks for posting all the links that really helps!
Title: Animations in XAML   
Name: Keyvan Nayyeri
Date: 12/14/2006 10:21:22 AM
Comment:
Fourth part of these tutorials about Animations in XAML:
http://aspalliance.com/1050_Animations_in_XAML
Title: Resources in XAML   
Name: Keyvan Nayyeri
Date: 12/14/2006 10:20:23 AM
Comment:
Third part of these tutorials about Resources in XAML:
http://aspalliance.com/1032_Resources_in_XAML
Title: Layout in XAML   
Name: Keyvan Nayyeri
Date: 12/14/2006 10:18:22 AM
Comment:
Second part of these tutorials about Layout in XAML:
http://aspalliance.com/1023_Layout_in_XAML
Title: Introduction to XAML   
Name: Keyvan Nayyeri
Date: 12/14/2006 10:17:27 AM
Comment:
First part of these tutorials, Introduction to XAML:
http://aspalliance.com/1019_Introduction_to_XAML






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2014 ASPAlliance.com  |  Page Processed at 10/24/2014 10:46:02 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search