1

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

Sumtraveller
  • 1,186
  • 2
  • 9
  • 13

2 Answers2

3

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
  • 627
  • 4
  • 8
  • 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 Aug 20 '09 at 19:58
  • 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 Aug 20 '09 at 20:01
3

Turns out the book I have WPF In Action with Visual Studio 2008, on page 149, has a very misleading diagram that seems to indicate that RoutedEvents can be raised in a parent node and that routed event tunnelled to child nodes.

That turns out to NOT be the case... 1/2 day blown b/c of bad diagram (fairly good book otherwise) [ ISBN : 1-933988-22-3 ]

Matt
  • 74,352
  • 26
  • 153
  • 180
Sumtraveller
  • 1,186
  • 2
  • 9
  • 13