#586 – Bubbling and Tunneling Events Are Typically Paired
June 22, 2012 4 Comments
Events defined for preexisting controls in WPF (e.g. a Button) are typically routed events–meaning that the event is propagated up or down the logical tree. Routed events can either be bubbling events (they propagate up the tree) or tunneling events (they propagate down the tree).
Many events in WPF related to user input are available in pairs, with both a bubbling and a corresponding tunneling event. For example, the KeyDown event (bubbling) has a corresponding PreviewKeyDown event (tunneling).
When events are paired, the tunneling events will typically fire first, followed by the paired bubbling event.
- User presses key
- Main Window sees PreviewKeyDown
- Outer StackPanel sees PreviewKeyDown
- Inner StackPanel sees PreviewKeyDown
- TextBox sees PreviewKeyDown
- TextBox sees KeyDown
- Inner StackPanel sees KeyDown
- Outer StackPanel sees KeyDown
- Main Window sees KeyDown