#591 – You Can Attach Any Routed Event to Any Control
June 29, 2012 Leave a comment
When you attach an event handler to a control, you most typically attach a handler for an event that is defined for that control. For example, the Button control has a Click event defined.
<Button Content="Like" Click="Button_Click"/>
In WPF, however, you can actually attach any routed event to any control–whether or not that control defines the event or not. For example, we can attach a handler for the Click event to a StackPanel object, using the full name for the ButtonBase.Click event.
A control won’t normally fire events that it doesn’t define, except that when events are routed, they can fire on every control higher up (or down) in the logical tree.
<StackPanel ButtonBase.Click="StackPanel_Click"> <Button Content="Like" Click="Button_Click"/> </StackPanel>
Now, when you click on the Button, both handlers that you define will see the Click event, because it bubbles up the logical tree.