BrainDump
  Home arrow BrainDump arrow Routing Events in WPF
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
BRAINDUMP

Routing Events in WPF
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2010-01-25

    Table of Contents:
  • Routing Events in WPF
  • Routing Strategies and Event Handlers
  • Routed Events in Action
  • Routed Events continued

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Routing Events in WPF


    (Page 1 of 4 )

    In this third part of a four-part series on the concepts you need to understand to work with Windows Presentation Foundation, you'll learn all about routing events. This article is excerpted from chapter three of the book Windows Presentation Foundation Unleashed, written by Adam Nathan (Sam's Publishing; ISBN: 0672328917).

    A Routed Event Implementation

    In most cases, routed events don’t look very different from normal .NET events. As with dependency properties, no .NET languages (other than XAML) have an intrinsic understanding of the routed designation. The extra support is based on a handful of WPF APIs.

    Listing 3.6 demonstrates how Button effectively implements its Click routed event. (Click is actually implemented by Button’s base class, but that’s not important for this discussion.)

    Just as dependency properties are represented as public static DependencyProperty fields with a conventional Property suffix, routed events are represented as public static RoutedEvent fields with a conventional Event suffix. The routed event is registered much like a dependency property in the static constructor, and a normal .NET event—or event wrapper—is defined to enable more familiar use from procedural code and adding a handler in XAML with event attribute syntax. As with a property wrapper, an event wrapper must not do anything in its accessors other than call AddHandler and RemoveHandler.

    LISTING 3.6   A Standard Routed Event Implementation

    public class Button : ButtonBase
    {
      // The routed event
     
    public static readonly RoutedEvent ClickEvent;

      static Button()
      {
       
    // Register the event
        Button.ClickEvent = EventManager.RegisterRoutedEvent("Click",
          RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Button));
        ...
      }

      // A .NET event wrapper (optional)
      public event RoutedEventHandler Click
      {
        add { AddHandler(Button.ClickEvent, value); }
        remove { RemoveHandler(Button.ClickEvent, value); }
      }

      protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
      {
        ...
        // Raise the event
        RaiseEvent(new RoutedEventArgs(Button.ClickEvent, this));
        …
      }
      …
    }

    These AddHandler and RemoveHandler methods are not inherited from DependencyObject, but rather System.Windows.UIElement, a higher-level base class of elements such as Button. (This class hierarchy is examined in more depth at the end of this chapter.) These methods attach and remove a delegate to the appropriate routed event. Inside OnMouseLeftButtonDown, RaiseEvent (also defined on the base UIElement class) is called with the appropriate RoutedEvent field to raise the Click event. The current Button instance (this) is passed as the source element of the event. It’s not shown in this listing, but Button’s Click event is also raised in response to a KeyDown event to support clicking with the spacebar or sometimes the Enter key.

    More BrainDump Articles
    More By Sams Publishing


     

    BRAINDUMP ARTICLES

    - Basic Operations and Registers in Assembly
    - Assembly Coding within Visual C/C++ IDE
    - New Microsoft Office Coming with a Twist
    - Microsoft`s FUSE Labs Unveils Spindex Social...
    - HP Slate with Windows 7: Dead or Alive?
    - Windows Phone 7 Mobile OS to Rival Android a...
    - Windows 7 Climbing the Charts, Fights for Ma...
    - Windows 7 Upgrades to Come at Cheaper Prices
    - Microsoft Being Inventive in New Ways to Off...
    - Microsoft Prepares 64 GB Zune HD for Upcomin...
    - Windows 7 Trial for Businesses Gets Extension
    - Microsoft Refuses to Follow Others` Lead on ...
    - Microsoft Promises Plenty of Surprises for E...
    - Are Microsoft Certifications Worth the Cost ...
    - Microsoft, NSF Open Cloud Computing to Scien...





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek