views:

702

answers:

2

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

+2  A: 

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.
Bryan Hunter
I think I wrongfully believed that a RoutingStrategy.Tunnel could communicate events to children of the source node (e.g. an parent control could raise an event that would "tunnel" down to a child).After re-reading the documentation you put here, all Routed Events seem to begin (bubble) or end (tunnel) or both (direct) at the source node.So in that case, my example just won't work using WPF RoutedEvents.A barking cat...
Sumtraveller
Also didn't help that I used BUTTON as part of my example (which only distracted from the real question) -- yeah, I've seen the PreviewMouseDown example... I was hoping for PARENT(where event occurs) --[tunnel]-->ToChild
Sumtraveller
+1  A: 
Sumtraveller