LA.NET [EN]

Jan 25

Attached properties

Posted in Silverlight      Comments Off on Attached properties

An attached property is an interesting concept. The docs define it as:

An attached property is intended to be used as a type of global property that is settable on any object

An attached property lets you define properties in an object which are only defined by its parent element. Once again, if you have any experience with WPF, you should take into account that the number of attached properties is much smaller than what you have in WPF (for instance, TextElement.FontStyle is implemented as an attached property in WPF, but it’s a simple dependency property in SL).

There is also another interesting gotcha (when compared with WPF): it seems like the XAML parser “knows” about several predefined attached properties (registered as core dependency properties) and lets you use them without registering them through the RegisterAttached method (ex.: Canvas.Left is an attached property, but it’s not registered through the RegisteredAttached method). I guess this is just another of those quirks which make Silverlight a much less “predictable” framework than WPF.

The simplest way to understand these type of properties is to look at an example: we’ll simply take a look at how you can use the Canvas.Left/Canvas.Top attached properties to position an element.

<Canvas Width="500" Height="500">
    <Rectangle Canvas.Left="10" 
Width="100" Height="20" x:Name="rect1"> <Rectangle.Fill> <SolidColorBrush Color="Blue" /> </Rectangle.Fill> </Rectangle> <Rectangle Canvas.Left="200" Width="100" Height="20"> <Rectangle.Fill> <SolidColorBrush Color="Red" /> </Rectangle.Fill> </Rectangle> </Canvas>

The Left and Top properties are exposed by the Canvas class. If you take a quick peek at the class, you’ll notice that each attached property is backed up by a dependency property (notice the TopProperty and LeftProperty fields). Attached properties can also be accessed from code through a pair of static methods on the form GetXXX/SetXXX, where XXX is the name of the property (in these case, you’ll have GetLeft and SetLeft for setting the Left attached property). Do notice that both GetXXX and SetXXX expect a reference to an object (the type of the object depends on the types of objects you’re extending with the attached properties). The SetXXX method expects a second parameter, generally of type Object, which is used for setting the value of the attached property.

Typically, custom attached properties need to be registered through the DepedencyProperty.RegisterAttached method (something which is done from within the static constructor). However,in the case of predefined Silverlight control’s attached properties,they’re simply registered with predefined IDs which (I think?) are used internally by the platform to identify the type of behavior which is associated with that specific property.

I was thinking about showing you how to create a custom attached property, but I’ll leave that for a future post. Stay tuned for more on Silverlight.