12.1. Routing Strategies
There are three types of routed event strategies in Avalon: bubble, direct, and tunnel:
A bottom-up routing strategy. The target element is first notified, then its parent, then its parent, and so on. If the event is marked Handled, then no other event handlers will be invoked. Microsoft suggests marking an event as Handled as soon as you know that there are no further elements along the route because there are performance advantages to keeping the codepath as short as possible.
The type of routing strategy used by Windows Forms and other Microsoft .NET libraries. Direct routing means that only the event source element is notified. If the event was raised by a Button, then only the Button in question will receive notification. Very few UI events in Avalon use the direct-routing strategy.
Works in the opposite direction as bubble; it starts at the root of the tree and works down, stopping with the target element. Tunneling events are prefixed in Avalon with the word Preview. Generally, there is a corresponding bubble event for each tunnel event. If the tunnel event is named PreviewKeyDown, then the bubble event is called KeyDown. Similarly, if there is a bubble event called KeyUp, then there is likely a tunneling event called PreviewKeyUp.
Because of tunneling and bubbling, parent elements often receive events when the source is one of their child elements. If necessary, the source of the event can be determined programmatically by accessing the Source property of the EventArgs parameter passed to the event handler, as in Example 12-3.
Not all events are routed. The XAML designer will need to know whether they are, because routed events can be used within styles to created triggers; non-routed events cannot. Events are specifically marked as routed or non-routed to assist the XAML designer with trigger creation.