Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

WPF - Catch Events Even if they are Already Handled

0.00/5 (No votes)
24 Feb 2010 1  
As you may actually know WPF introduced the routed events. These last are no more specific to a single control but they are routed inside the tree of your controls. If you want to stop an event, you can mark it as Handled. If so, the routing engine will stop to propage it. In fact this is...
As you may actually know WPF introduced the routed events. These last are no more specific to a single control but they are routed inside the tree of your controls.

If you want to stop an event, you can mark it as Handled. If so, the routing engine will stop to propage it. In fact this is just an illusion because the engine will only stop leveraging your handlers.

But sometimes, for example when you are using a control from third parties, you want to catch the events even if marked as handled. Here is the little piece of code to use:
C#
anyUIElement.AddHandler(
    UIElement.MouseEnterEvent,
    (RoutedEventHandler)OnMouseEnterCallMeAlways,
    true
    );

This method can be called on any UIElement, in code only. The important part here is the Boolean (true) which tells the engine to call the handle even if the events is marked as handled.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here