Attached events are used to handle the event from a different control mostly from parent control rather than from the same control which has that event. This functionality is possible only because of bubbling and tunneling event concepts.
Understanding bubbling and tunneling events
For example, if you have 2 button controls inside the stack panel. If both of that controls are doing some functionality and you want to handle the click event of those 2 buttons from a single method. In that case you can use the click event of the ButtonBase class as an attached event in the StackPanel and raise the event when any one button got clicked. Notice one thing that, there is not click event in stack panel. Still we are able to handle the button click event from stack panel only because or bubbling and tunneling routed events. Otherwise the click event of the button will not come to the stack panel and we cannot handle it.
Below example shows how to use attached event.
<Button Content="Yes" Height="23" Name="btnYes" Width="75" />
<Button Content="No" Height="23" Name="btnNo" Width="75" />
private void Attached_ButtonClick(object sender, RoutedEventArgs e)
Button source = (Button)e.Source;
if (source.Name == "btnYes")
Debug.WriteLine("'Yes' button clicked");
else if (source.Name == "btnNo")
Debug.WriteLine("'No' button clicked");
'Yes' button clicked
'No' button clicked