CSharp Certification, Development, and Training | Recent changes | View source | Page history

Printable version | Disclaimers | Privacy policy

Not logged in
Log in | Help
 

WPF Concepts—Routed Event Implementation

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio

CSharp-Online.NET:Articles
.NET Articles

Important New WPF Concepts

© 2007 Sams Publishing

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.


Previous_Page_.gif Next_Page_.gif

Retrieved from "http://en.csharp-online.net/WPF_Concepts%E2%80%94Routed_Event_Implementation"

Accessed: 2,191 times Modified: 15:43, 27 February 2007. © CSharp-Online.NET


[CSharp Certification, Development, and Training]
C# Certification, Development, and Training (Home)
ASP.NET
C# and Visual C#
C# .NET Articles
C# .NET Glossary
C# .NET Resources
C# .NET Reviews
C# .NET Tutorials
C# .NET Videos
Microsoft .NET Framework
Microsoft Visual Studio .NET

View source
Discuss this page
Page history
What links here
Related changes

Special pages
Bug reports