AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=1088&pId=-1
Events in Windows Presentation Foundation
page
by Keyvan Nayyeri
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 27548/ 39

Introduction

Like any graphic user interface engine, Windows Presentation Foundation, a collection of rich APIs which provide appropriate functionalities for building user interfaces in .NET 3.0, has some ways to handle events.  End users interact with user interfaces and have a wide range of mouse or keyboard events to deal with user interface in order to choose items and fire events.  Then developer's code should define appropriate event handlers to handle these events.  Windows Presentation Foundation, in conjunction with XAML, comes with several ways to handle events and provides rich APIs to work with them.

In this article I want to give an introduction to events in Windows Presentation Foundation and a brief description of related topics in it.

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.

Event Argument

Event handlers in Windows Presentation Foundation are very similar to handlers in traditional Windows Forms.  An object as sender and an event argument object are two parameters which form the typical signature for event handlers.

Each event handler has some information about the specific event type that it represents so you can use it in your code to accomplish several tasks and get helpful information about the event and the element that has received the event.  Properties for each event argument type may vary from one another but some properties are common between event arguments.

Here I give a list of eight event argument types with a description about the purpose and usage of each one.

·         RoutedEventArgs: This event argument type is a base type for some other argument types.  RoutedEventArgs, Itself, is the argument type that will be passed to some events like Click and Close.

·         KeyEventArgs: An event argument type for key events such as KeyUp or KeyDown.

·         MouseEventArgs: An event argument type to work with the mouse and its events.  Generally it's useful when you want to work with mouse states.

·         MouseButtonEventArgs: An extension of MouseEventArgs which adds some functionality to work with mouse button events.

·         TextChangedEventArgs: An event argument which provides properties for the TextChanged event.  It's inherited from RoutedEventArgs.

·         SelectionChangedEventArgs: An event argument which provides properties for SelectionChanged event.  It's inherited from RoutedEventArgs.

·         ScrollChangedEventArgs: An event argument which provides properties for working with scrollbar events.  It's inherited from RoutedEventArgs.

·         DependencyPropertyChangedEventArgs: This event argument is passed to events when a dependency property is changed.

Events

There are several defined events in Windows Presentation Foundation.  Each event can be routed or not, can apply to some specific elements and has one of above event argument types.  You can find full details about each event in Windows Presentation Foundation documentation on MSDN.

There are various events available but many of these events have a specific application.  You have to choose the best event for your needs but fortunately working with all events is similar.

Summary

In this article, the last part of my tutorials about XAML and Windows Presentation Foundation, I covered another fundamental concept about events.  We discovered three main types of routing strategies and gave a description about event arguments and different events.  Event handling is enhanced in Windows Presentation Foundation and can provide powerful user interfaces which you may have seen already or will see in near future.



©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-08 1:18:58 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search