|
Hello, I've managed to setup an example working for both RoutedEvent and AttachedEvent but it appears the difference between the two is just syntactical as "RoutedEvent" handler can be declared in XAML in same syntax as "AttachedEvent". (I understand the difference between AttachedProperty vs DependencyProperty clearly)
Because of formatting issue with XAML - detail here:
http://forums.codeguru.com/showthread.php?534049-RoutedEvent-vs-AttachedEvent&p=2103409#post2103409[^]
Any idea anyone? Thanks
dev
|
|
|
|
|
Please don't raise the same question in multiple locations. You've just made it harder for yourself to get a cohesive answer. I'm not going to create an account on CodeGuru just to answer this question when the forums here are perfectly capable.
On the surface, it would appear that the two items are the same because they have the same syntax. Well, this is true to the extent that an attached event IS a routed event. A routed event, however, is not an attached event. In other words, an attached event is a specialised form of routed event.
So, what is an attached event? Well, basically, it's an event that can be attached to another element without the other element having to be aware that this capability is present. If you think about it as being like an attached property for events then it starts to make sense. So, why do we have this? Well, it allows you to extend WPF through composition rather than inheritance.
|
|
|
|
|
Pete O'Hanlon wrote:
So, what is an attached event? Well, basically, it's an event that can be attached to another element without the other element having to be aware that this capability is present. If you think about it as being like an attached property for events then it starts to make sense. So, why do we have this? Well, it allows you to extend WPF through composition rather than inheritance.
I understand perfect "Grid.Row" is an "Attached Property" that it's not a property of "TextBox" itself for instance. I understand analogy when this concept extended to "Events". However - what's confusing is the SYNTAX: you can define an event handler to a RoutedEvent the same way/syntax as you would with AttachedEvent.
But if you see the sample I gave in MainWindow.xaml, I can subscribe to "AttachedEvent" and "RoutedEvent" from xaml in same syntax - the syntax which does not require "Instance" of "GrandChild" (i.e. "MainWindow" does NOT implement event GrandChildRoutedEvClick itself, which is declared in "GrandChild")
From MainWindow.xaml, "RoutedEvent" handler setup like this:
<br />
local:GrandChild.GrandChildRoutedEvClick="GrandChild_GrandChildRoutedEvClick"<br />
From MainWindow.xaml, "AttachedEvent" handler setup like this:
<br />
local:GrandChild.GrandChildAttachedEvClick="GrandChildAttachedEvClickHandler" <br />
Despite in "MiddleChild.xaml", I setup handler to "RoutedEvent" the "Propery way"... what's real diff between Routed/AttachedEvent besides phylosophical or syntactical ...?
<br />
[local:GrandChild x:Name="Junior" VerticalAlignment="Stretch" GrandChildRoutedEvClick="GrandChild_GrandChildRoutedEvClick"/]<br />
What I am saying is, you can define a DockStateChanged event with syntax of a "RoutedEvent" - not "AttachedEvent", and in some parent control couple layers up, setup event handler using syntax as you would consuming an "AttachedEvent"
dev
modified 30-Jan-13 5:11am.
|
|
|
|
|
As I said. An attached event is a specialist form of routed event. I don't know how I can say it any clearer - other than an attached event IS A routed event that is used for a specific purpose that routed events aren't designed for.
|
|
|
|
|
Don't mention it. I hope it's clearer now.
|
|
|
|
|
I heard ya, I was looking for some sort of confirmation.
dev
|
|
|
|