WPF How to create CUSTOM tunel routedevent?
-
19-09-2019 - |
Question
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
- using PreviewMouseDown (not the same of course and likely dangerous since not all mouse-downs are meant to become clicks).
- Writing a TunnelButton that raises a PreviewClick and then a Click.
OTHER TIPS
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 ]