Attached property – WPF. Also diff b/w attached and Dependdency prop

An attached property is intended to be used as a type of global property that is settable on any object. Attached properties are a special form of dependency property.  They exist so that a child element can store a value associated with a property defined on an ancestor element.  This is commonly used in the interaction between elements and the WPF layout infrastructure,.


The following example shows how the DockPanel.Dock attached property can be set on any element (in this case, a Button) within a DockPanel.

<Button x:Name=”btn” DockPanel.Dock=”Left”/>

The equivalent C# code for setting the attached Dock property on a Button is:

DockPanel.SetDock( btn, Dock.Left );

Attached properties differ from normal dependency properties in several ways.  The most noticeable is that they are not wrapped in CLR properties (i.e. standard .NET properties).

Other differences between attached properties and normal dependency properties include the methods used to register them with the property system (DependencyProperty.RegisterAttached and .RegisterAttachedReadOnly instead of .Register and .RegisterReadOnly), and where the property values are stored (on the element which consumes the property instead of the element which declares the property).

The value which an element has for an attached property is stored by that element, not by the element which exposes the attached property.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s