Subscribe to routed events even if they are handled

 
 
  • Gérald Barré

WPF introduces routed events. Unlike regular events, routed events are not specific to a single control; they travel through the entire control hierarchy.

To stop an event, mark it as handled by setting the Handled property to true in a handler. The WPF routing engine stops calling subsequent handlers. However, the event itself continues to travel up the tree; only the handlers are skipped.

Sometimes you may want to receive these handled events anyway, for example when using a control from a third-party library. You can do this by passing handledEventsToo: true to AddHandler:

C#
myControl.AddHandler(
    routedEvent: TheEvent,
    handler: (RoutedEventHandler)MyHandler,
    handledEventsToo: true);

Do you have a question or a suggestion about this post? Contact me!

Follow me:
Enjoy this blog?