EventManager.RegisterClassHandler Method (Type, RoutedEvent, Delegate) (System.Windows)

Switch View :
ScriptFree
.NET Framework Class Library
EventManager.RegisterClassHandler Method (Type, RoutedEvent, Delegate)

Registers a class handler for a particular routed event.

Namespace:  System.Windows
Assembly:  PresentationCore (in PresentationCore.dll)
Syntax

Visual Basic
Public Shared Sub RegisterClassHandler ( _
	classType As Type, _
	routedEvent As RoutedEvent, _
	handler As Delegate _
)
C#
public static void RegisterClassHandler(
	Type classType,
	RoutedEvent routedEvent,
	Delegate handler
)
Visual C++
public:
static void RegisterClassHandler(
	Type^ classType, 
	RoutedEvent^ routedEvent, 
	Delegate^ handler
)
F#
static member RegisterClassHandler : 
        classType:Type * 
        routedEvent:RoutedEvent * 
        handler:Delegate -> unit 

Parameters

classType
Type: System.Type
The type of the class that is declaring class handling.
routedEvent
Type: System.Windows.RoutedEvent
The routed event identifier of the event to handle.
handler
Type: System.Delegate
A reference to the class handler implementation.
Remarks

Class handling is a feature that is available for routed events, including attached events that are implemented with routed event backing. A class handler is like a static handler that exists for all instances of the class. Because the handler is static, you cannot change instance properties directly with a class handler, but you can access instances through the sender parameter and/or the event data.

Class handlers are invoked before instance handlers. You can implement a class handler that has the behavior of marking the event as handled. Therefore, instance handlers for a class-handled event are not invoked unless the instance handlers register specifically for handled events.

Many of the WPF base element events provide class handling virtual methods. By overriding these methods in classes that inherit the base classes, you can implement class handling without calling RegisterClassHandler in static constructors. These class handling methods typically exist for input events and have names that start with "On" and end with the name of the event being class handled.

For more information about class handling, see Marking Routed Events as Handled, and Class Handling.

Using this signature, class handlers will be registered to invoke only in response to unhandled events. You can also register class handlers to invoke even if the event arguments are marked handled, by using the RegisterClassHandler(Type, RoutedEvent, Delegate, Boolean) signature, with handledEventsToo set to true.

Examples

The following example adds a handler for PreviewMouseLeftButtonDown, calling RegisterClassHandler.

Visual Basic

	Shared Sub New()
	  EventManager.RegisterClassHandler(GetType(MyEditContainer), PreviewMouseRightButtonDownEvent, New RoutedEventHandler(AddressOf LocalOnMouseRightButtonDown))
	End Sub
	Friend Shared Sub LocalOnMouseRightButtonDown(ByVal sender As Object, ByVal e As RoutedEventArgs)
	  MessageBox.Show("this is invoked before the On* class handler on UIElement")
	  'e.Handled = True //uncommenting this would cause ONLY the subclass' class handler to respond
	End Sub


C#

static MyEditContainer()
{
  EventManager.RegisterClassHandler(typeof(MyEditContainer), PreviewMouseRightButtonDownEvent, new RoutedEventHandler(LocalOnMouseRightButtonDown));
}
internal static void LocalOnMouseRightButtonDown(object sender, RoutedEventArgs e)
{
  MessageBox.Show("this is invoked before the On* class handler on UIElement");
  //e.Handled = true; //uncommenting this would cause ONLY the subclass' class handler to respond
}


Version Information

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
See Also

Reference

Other Resources