4

HEllo, can someone explain me what class-level event handler is in WPF? I use routed events in WPF but currently I read a book and I found the author mentions about class-level event handler. What is the practical use of this technique?

niao
  • 4,972
  • 19
  • 66
  • 114

1 Answers1

8

Think of class handlers as static event handlers for a routed event. You might want to register such a handler if you want, for example, handle all mouse down events without any particular instance of the object involved. You would typically register it in a static constructor of a class:

static MyWindow()
{
    EventManager.RegisterClassHandler(typeof(MyWindow), PreviewMouseLeftButtonDownEvent, new RoutedEventHandler(OnMouseLeftButtonDown));
}

See also:

http://msdn.microsoft.com/en-us/library/ms597875.aspx

http://karlshifflett.wordpress.com/2008/04/22/wpf-sample-series-eventmanagerregisterclasshandler/

Pavlo Glazkov
  • 20,498
  • 3
  • 58
  • 71