Page view counter

Dependency Properties or Attached Properties

Silverlight lets you create a property in an object (such as a rectangle) that derives from an ancestor element (such as a canvas). That is why we can write

<Rectangle Canvas.Top="10" />

Canvas.Top is a property of the Canvas, the ancestor element in the object tree.  Tim Heuer, one of the smarter folks I know, calls this a Dependency Property. I call this an attached Property. So I did a little spelunking to find out which is right, and here's what I found.  


“Attached properties” are defined in this Quick Start as follows:


 Getting or Setting Attached Properties
An attached property is a XAML language concept whereby elements can be assigned attributes for a property, even though that property is not present in the members list of the code type that backs the element.   

Ahhhh, the sweet taste of vindication. I should have stopped there, but then I found
 this blog (admittedly about WPF, but I think it applies), which shows Attached Properties as a subset of Dependency Properties:  


Dependency properties are used when the resolution of a property’s value is based on other properties or runtime settings (such as operating system settings).  Here are some important features of dependency properties: 
  • Value resolution – DPs are used to form a system which can determine the actual property value based on various runtime information.  The resolution process has an order of precedence it assigns to various environmental contexts in which the property might exist.  For example, if the DP is being modified by an animation then the value supplied by the animation is the resolved value, but if it is not animated, then the value is derived from elsewhere.
  • Self-contained validation – DPs can have custom callback methods execute when the property value has changed.  These callbacks can validate the new value or coerce the new property value into something acceptable, according to the semantics of the property.
  • Default values – DPs provide a consistent mechanism for associating a default value with a property.  A DP’s default value has the lowest precedence in the value resolution process, meaning that if there is no other way to determine the property value, then the default value will be used.
  • Property meta-data – The property system knows how a DP should behave based on meta-data supplied at the time the property is registered with the system.  Subclasses can tweak a DP by overriding the property’s meta-data, instead of completely re-implementing the property itself.  It is interesting to note that this meta-data is not stored in attributes, partly because the performance impact associated with using reflection to manipulate the meta-data was unacceptable.
  • XAML friendly – Just like normal properties, DPs can be set in XAML.
  • Value inheritance – Any DP can be given the ability to inherit its value from the property setting on an ancestor element, in the logical tree.  This provides similar functionality to the ambient properties used in Windows Forms.  Value inheritance is useful in many situations, such as propagating a data source down the element tree, font settings, flow direction (right-to-left) settings, etc.
  • Attached properties – A form of dependency property which allows a child element to store a value associated with a property defined on an ancestor element. 
 My conclusion, after all of 10 minutes of research....  When you write:
                <Rectangle Canvas.Top=”15”> 

It is correct to call it  either an Attached property or a Dependency Property, in the sense that if All Throgs are Thrains and this is a Throg then it is also a Thrain. 

 

 

 

Published Wednesday, September 19, 2007 8:27 AM by jesseliberty

Comments

# Dependency Properties or Attached Properties - Jesse Liberty - Silverlight Geek

Pingback from  Dependency Properties or Attached Properties - Jesse Liberty - Silverlight Geek

# Dependency Properties or Attached Properties

Silverlight lets you create a property in an object (such as a rectangle) that derives from an ancestor

Wednesday, September 19, 2007 10:28 AM by Test

# Silverlight Cream for September 19, 2007

Silverlight Cream for September 19, 2007

Wednesday, September 19, 2007 12:26 PM by WynApse

# re: Dependency Properties or Attached Properties

A couple of points:

Attached properties in XAML in general have nothing to do with either derivation or containment. So when you say that you can have property

"that derives from an ancestor element (such as a canvas)"

that's not really what's going on from the XAML perspective. It so happens that in the Silverlight XAML vocabulary all the attached properties happen to be defined by ancestor elements, but that's just a coincidence. It's not a feature of attached properties in general, it just happens to be a feature of the small set of attached properties Silverlight happens to support.

If you look at the WPF XAML vocabulary, it includes all the attached properties supported by Silverlight, but it also includes a load more. For example, there's the FocusManager.FocusedElement attached property. You'll never see anything be the descendent of a FocusManager, because FocusManager is a static class - you can't create one so it can't be the ancestor of anything in a visual tree. And yet you can set the FocusManager.FocusedElement on elements in a WPF visual tree.

(So Josh's description is wrong I'm afraid. He's talking about WPF, which has several attached properties that don't match his description.)

So attached properties have nothing to do with containment. An attached property is simply one that can be applied to an element of a different type than the type that defines the property. So Canvas defines a Left property, but allows that property to be applied to other things such as Rectangle. And when Canvas's Left property is applied to a non-Canvas element such as Rectangle, we call that an attached property.

The attached properties defined by Canvas only mean anything useful when they are attached to the children of a Canvas. However, in WPF, you're free to attach them to anything you like. They are simply ignored if you use them anywhere other than on the children of a Canvas.

And derivation doesn't come into the picture at all. I'm not sure why you mentioned that.

Thursday, September 20, 2007 7:08 AM by Ian Griffiths

# re: Dependency Properties or Attached Properties

Ian,

Thank you for the very thoughtful analysis.

The use of the term "derivation" was too casual; the term has a specific meaning and I should have chosen a different word.

It is valuable, however, to have the second blog entry corrected; I frankly didn't know that about WPF, and I appreciate your taking the time to sort that out.

-jesse

Thursday, September 20, 2007 8:24 AM by jesseliberty

# Silverlight Cream for September 21, 2007

Silverlight Cream for September 21, 2007

Friday, September 21, 2007 2:40 PM by WynApse

# re: Dependency Properties or Attached Properties

...in fact it turns out to me more complex than I thought, as I discovered since I wrote the previous reply.

It is possible for a property to declare that it's only supposed to be applicable to child elements. If the Get accessor has the AttachedPropertyBrowsableForChildrenAttribute applied, it's declaring that it's only meaningful when applied to children of the defining type.

Not that anything enforces this right now. Indeed, it's only intended as a design-time hint for where the property should be suggested.

Sunday, September 23, 2007 12:14 PM by Ian Griffiths

# SilverSpud &raquo; Blog Archive &raquo; Dependency properties, conceptually explained

Pingback from  SilverSpud  &raquo; Blog Archive   &raquo; Dependency properties, conceptually explained