Implements WPF routed events including Bubbling, Tunneling, and Direct strategies. Use when creating custom routed events, handling event propagation, or understanding Preview events.
View on GitHubchristian289/dotnet-with-claudecode
wpf-dev-pack
January 23, 2026
Select agents to install to:
npx add-skill https://github.com/christian289/dotnet-with-claudecode/blob/main/wpf-dev-pack/skills/routing-wpf-events/SKILL.md -a claude-code --skill routing-wpf-eventsInstallation paths:
.claude/skills/routing-wpf-events/# WPF Routed Events Patterns
Understanding and implementing WPF's routed event system for event propagation through element trees.
## 1. Routing Strategies Overview
```
Window (Root)
│
┌──────────────────┼──────────────────┐
│ │ │
Grid Border StackPanel
│ │ │
Button TextBox ListBox
│
ContentPresenter
│
TextBlock (Event Source)
Tunneling (Preview): Window → Grid → Button → ContentPresenter → TextBlock
Bubbling: TextBlock → ContentPresenter → Button → Grid → Window
Direct: Only TextBlock
```
---
## 2. Routing Strategy Types
| Strategy | Direction | Event Name Pattern | Use Case |
|----------|-----------|-------------------|----------|
| **Tunneling** | Root → Source (downward) | PreviewXxx | Input validation, cancellation before processing |
| **Bubbling** | Source → Root (upward) | Xxx | Normal event handling |
| **Direct** | Source only | Xxx | Events that don't propagate (MouseEnter, MouseLeave) |
---
## 3. Tunneling and Bubbling Example
### 3.1 XAML Setup
```xml
<Window PreviewMouseDown="Window_PreviewMouseDown"
MouseDown="Window_MouseDown">
<Grid PreviewMouseDown="Grid_PreviewMouseDown"
MouseDown="Grid_MouseDown">
<Button PreviewMouseDown="Button_PreviewMouseDown"
MouseDown="Button_MouseDown"
Content="Click Me"/>
</Grid>
</Window>
```
### 3.2 Event Handler Order
```csharp
// Execution order when Button is clicked:
// 1. Window_PreviewMouseDown (Tunneling)
// 2. Grid_PreviewMouseDown (Tunneling)
// 3. Button_PreviewMouseDown (Tunneling)
// 4. Button_MouseDown (Bubbling)
// 5. Grid_MouseDown (Bubbling)
// 6. Window_MouseDown (Bubbling)
private void Window_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
Debug.WriteLine("1. Window PreviewMouse