Home > c# > Attached Properties

Attached Properties

Attached property is a special type of dependency property that is intended to be used as global property. One of the purpose of Attached property is to allow child elements to set different value for property which has been defined on parent element. Examples of Attached properties are DockPanel.Dock, Grid.Row, Grid.Column, Canvas.Left, Canvas.Top etc. The use of Attached property is similar to static property and it can not be accessed through instance object.

Attached properties don’t implement wrappers for property because Attached property may not be part of the clr namespaces for the instances on which it is set. However, attached property implementer provides the accessors to access the property value.

DockPanel dockPanel = new DockPanel();

Button myButton = new myButton();

dockPanel.Childern.Add(myButton);

DockPanel.SetDock(myButton, Dock.Left);

Defining  attached property is almost same as defining the dependency property. The only change is during the registration instead of ‘Register’ use ‘RegisterAttached’ method of DependencyProperty. Also, don’t implement the property wrapper for the attached property. But instead of property wrapper implement the Get and Set accessor for the property.

Categories: c#
  1. No comments yet.
  1. No trackbacks yet.