Question

I am studying Silverlight (mainly for Windows Phone Development). I read Silverlight 2 Unleashed and currently reading Silverlight 4 unleashed. The latter has a chapter on dependency property. As I got from this chapter is that dependency object (with dependency properties) is used for UI control objects to support animation and data binding systems. Ok. That's clear.

Now, as the chapter goes on, the attached property is discussed. The main reason to use it (as stated in the book as well as on the internet) is to prevent making subclass and add attached properties to the object in concern. And since attached properties are already dependency properties, attached behaviour is added too.

The question now: what do I have to do that? What's wrong with subclassing? and really, why there is alot of code when dealing with attached property?

Was it helpful?

Solution

The question now: what do I have to do that?

Let's look at a great example - layout.

Take the Grid class. If we wanted to support this, every control would need to have a GridRow and GridColumn property. However, attached properties allow us to attach Grid.Row to any control, which in turn allows the Grid to layout that control within itself properly.

Attached properties all work in this basic way - they allow you to "attach" something that defines or helps some behavioral feature work properly without changing the "something" itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top