wpfroutedevent

WPF How to create CUSTOM tunel routedevent?


I need a simple example of a Tunnel Routed Event tunnelling from a parent control to a child control.

(THIS IS NOT REAL CODE) -- in fact, the deeper I go, the more I think that the XAML is wrong -- probably should NOT sign up for the tunnelled event in XAML on the child node (not sure?)

<PARENT>    
   <MyControl DoSomethingOnUserAction="raiseTunnelEvent"> HELP </MyControl >    
   <CHILD> I SHOULD HANDLE tunnelled event </CHILD> 
</PARENT>

Simple, concise example would be helpful.

Thanks, Alan


Solution

  • Not sure, but you may be wanting a cat to bark.

    The RoutedEvent ClickEvent of Button (from PresentationFramework) is declared as:

    public static readonly RoutedEvent ClickEvent = 
        EventManager.RegisterRoutedEvent("Click", 
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof
        (ButtonBase));
    

    Note the readonly RoutingStrategy of Bubble.

    The following may help with understanding Tunnel, Bubble, and Direct: msdn.microsoft.com/en-us/library/system.windows.routingstrategy.aspx

    And this should take you the rest of the way: msdn.microsoft.com/en-us/magazine/cc785480.aspx

    A tip: by convention tunneling events in WPF begin with "Preview" (e.g.- "PreviewExplode". If the event doesn't begin with "Preview" it probably doesn't use the tunnel RoutingStrategy. Also you will usually see a Tunnel and Bubble paired with the Tunnel firing first then the Bubble as in "PreviewExplode" followed by "Explode".

    If you need to have a Button's Click tunnel, you might consider

    1. using PreviewMouseDown (not the same of course and likely dangerous since not all mouse-downs are meant to become clicks).
    2. Writing a TunnelButton that raises a PreviewClick and then a Click.