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.
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.
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.
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.