what's the difference between Routed Events and Attached Events?

Posted by vverma01 on Programmers See other posts from Programmers or by vverma01
Published on 2012-11-09T08:31:44Z Indexed on 2012/11/09 11:20 UTC
Read the original article Hit count: 257

I tried to find through various sources but still unable to understand difference between routed events and attached events in WPF. Most of the places of reference for attached event following example is used:

<StackPanel Button.Click="StackPanel_Click">
    <Button Content="Click Me!" Height="35" Width="150" 
    Margin="5" />
</StackPanel>

Explained as: stack panel do not contain Click event and hence Button.Click event is attached to Stack Panel.

Where as msdn says:

You can also name any event from any object that is accessible through the default namespace by using a typename.event partially qualified name; this syntax supports attaching handlers for routed events where the handler is intended to handle events routing from child elements, but the parent element does not also have that event in its members table. This syntax resembles an attached event syntax, but the event here is not a true attached event. Instead, you are referencing an event with a qualified name.

  • According to MSDN information as pasted above, the above example of Buttons and StackPanel is actually a routed event example and not true attached event example.

  • In case if above example is truly about usage of attached event (Button.Click="StackPanel_Click">) then it's in contradiction to the information as provided at MSDN which says

Another syntax usage that resembles typename.eventname attached event> syntax but is not strictly speaking an attached event usage is when you attach handlers for routed events that are raised by child elements. You attach the handlers to a common parent, to take advantage of event routing, even though the common parent might not have the relevant routed event as a member.

A similar question was raised in this Stack Overflow post, but unfortunately this question was closed before it could collect any response.

Please help me to understand how attached events are different from routed events and also clarify the ambiguity as pointed above.

© Programmers or respective owner

Related posts about .NET

Related posts about wpf