Understanding WPF Dependency Property and Attached Property


One of my biggest challenge to understand WPF is the concept of attached property and dependency property. Every time I ask someone, I have a slightly different answer.

I think I have a good grasp of the concept, this is how I can explain how dependency property and Attached Property works.

Dependency Property

This is WPF property system, even though this is a property, it is not the same as regular property. The properties are not stored in an internal field, but it is stored in an internal storage system that stores per-instance value of the property.

The dependency property is a public static read-only field that must be registered first. After it has been registered, this static property is used to get and set the value in the internal storage system.

Attached Property

This is also dependency property. The internal storage system takes a DependencyProperty as key to get and set value. Attached property allows an object to to store a value using key that belongs to another class.

Imagine WPF property system is a property bag, and the property bag can take any kind of values. In most cases, the key for this property bag is defined in the class itself, but for attached property, the key is defined in other class.

To illustrate:

Imagine a person represents a class with dependency property. Let says his name is John. Whenever a dependency property is set, John writes this property in his notebook. When the property value is requested, he reads the note and return the value. John can only writes value for the property that he knows.

Now, imagine there is another person, Jane. She has her notebook, and she has a note, that one property in her notebook is also an attached property. The meaning of this property is relevant only for Jane, but everyone can keep it in their notebook. Everyone in the office knows about the attached properties.

John is assigned a property value, and he notices that this is value is for a property belongs to Jane and it is an attached property. He does not know what to do, but because this is an attached property, he writes it down in his notebook.

Jane asked John, if he has a value for a property which she understands, John reads his notebook, and give Jane the value. Based on that, Jane can do something.

Now replace John with Button class, Jane with Grid class, and the attached property with Grid.Row and Grid.Column.


Comments (6)

  1. Recai says:

    I think also another difference is if you have a sealed class such as WebBrowser you need to use attached property instead of dependency property.

  2. Ajit says:

    Very helpful to grasp the concept 🙂

  3. Vishal says:

    Nice explanation. Can you please tell me on what basis Jane ask John whether he has value of this attach property. Do Jane has knowledge of which persons to ask for values for this attach property?

    Who stores this information that the value of this attached property is set in a button or a text or a panel?

  4. Lisa says:

    This is the best explanation I found so far. 🙂

  5. Lisa says:

    Although, about this part:

    "John is assigned a property value, and he notices that this is value is for a property belongs to Jane and it is an attached property. He does not know what to do, but because this is an attached property, he writes it down in his notebook.

    Jane asked John, if he has a value for a property which she understands, John reads his notebook, and give Jane the value. Based on that, Jane can do something."

    Should it be more like:

    "

    John is assigned a property value, and he notices that this is value is for a property belongs to Jane and it is an attached property. Because this is an attached property, he writes it down in his notebook.

    But he does not know what to do, so he asks the owner of this property – Jane. Based on that, Jane can do something.

    "

  6. My apology for my late responds.

    @Vishal: Jane does know which person to asks for this attached property. Grid object inspect all elements inside the Grid.Children, whether they have Grid.Column or Grid.Row properties.

    @Lisa: I believe the Grid object is the one that inspects all children objects, for those attached properties, so using my illustration, John does not proactively ask Jane about this property.

    Look into how you define Grid and children inside the grid. The children objects do not know whether they are inside a grid, a stackpanel, or something else, and they do not have a reference to lookup the parent object. The parent object, however, does have a reference to the children objects.

Skip to main content