WPF FAQ
Get Microsoft Silverlight
Skip Navigation Links

Attached Properties

An interesting side-effect of the sparse storage system used by WPF is that the property does not neccesarily need to be stored in the class that declares it. As you saw in the article on dependency properties, the static declaration in the object is used as a key in a map of values so there is nothing to stop a different dependency object "hosting" this value.

This technique of attaching a property to a different dependency object is partucularly useful in the context of WPF graphics because it means that an object might declaritively provide information for the use of parent objects higher up the chain of graphical wigets.

Two classic examples of attached properties are the Grid.Column or Grid.Row properties which may be used by an object to declare in which grid-cell they should be hosted and the Canvas.Top and Canvas.Left properties that declare how a graphical object should be positioned on a canvas.

An attached property may be hosted in any object but only those used in a specific context, defined by the object that declares the property, will be useful. You can for example define Canvas.Left in the XAML of your main window but it would be useless because there would never be an enclosing Grid control to take notice of the property.

 If you ever need to create your own WPF controls you may need to create your own attached property. This is accomplished using the RegisterAttached method You can also include information about how the property might be used by including a FrameworkPropertyMetadataOptions value.