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. 

 

 

 

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen Pluralsight & LinkedIn Learning courses. He was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Xamarin Certified Mobile Developer and a Xamarin MVP and a Microsoft MVP.
This entry was posted in z Silverlight Archives. Bookmark the permalink.

20 Responses to Dependency Properties or Attached Properties

  1. I like reading aan article that will make people think.
    Also, thanks for allowing for me to comment!

  2. I’m truly enjoying the design and layout of your blog.
    It’s a very easy on the eyes which makes it much more pleasant for me to come here and visit more often.
    Did you hire out a designer to create your theme? Fantastic work!

  3. Excellent blog post. I absolutely appreciate this website.
    Keep it up!

  4. Hi there! Someone in my Facebook group shared this site with us so I came to take a look.
    I’m definitely loving the information. I’m bookmarking and
    will be tweeting this to my followers! Fantastic blog and outstanding
    style and design.

  5. I like the valuable info you provide in your articles.

    I’ll bookmark your blog and check again here frequently. I’m
    quite sure I’ll learn lots of new stuff right here!
    Best of luck for the next!

  6. This site definitely has all the information I wanted about this subject and didn’t know
    who to ask.

  7. Lillian says:

    whoah this weblog is wonderful i love studying your
    posts. Keep up the good work! You already know, a lot of individuals are looking round for this info, you could aid them greatly.

    My web site :: IT Support Services (Lillian)

  8. Earnestine says:

    Hi, I do think this is an excellent web site. I stumbledupon it 😉
    I’m going to come back once again since i have book-marked it.
    Money and freedom is the best way to change, may you be rich
    and continue to help other people.

    Visit my website :: Managed IT Services (Earnestine)

  9. Hey! I’m at work surfing around your blog from my new iphone 4!
    Just wanted to say I love reading through your blog and look forward to all
    your posts! Keep up the excellent work!

    Look into my web-site: Rugged Tablets (shehzadakbar.com)

  10. Hello there! I just want to give you a huge thumbs up
    for your great info you have here on this post. I’ll be coming back to your web
    site for more soon.

    My site HomePage (http://www5e.biglobe.ne.jp/~hot-oita/cgi-bin/yybbssoo/yybbs.cgi)

  11. If you would like to take a good deal from this piece of writing then you have to apply such techniques to your won
    webpage.

    my page; search engine optimization

  12. Cat behaviour issues: Do you want a solution watch our new blog

  13. AG says:

    On the second thought, dependency prop. has to be declared in supporting code behind and use mechanism to register itself in property system. While attached allows component to hook itself to parent component in visual tree and there are no need for supporting code

  14. AG says:

    On the second thought, dependency prop. has to be declared in supporting code behind and use mechanism to register itself in property system. While attached allows component to hook itself to parent component in visual tree and there are no need for supporting code

  15. AG says:

    The clearness of explanation is outstanding.

  16. Simply wish to say your article is as surprising. The clearness to your publish is just spectacular and that i can think you’re an expert in this subject. Well together with your permission let me to clutch your RSS feed to keep updated with approaching post. Thanks 1,000,000 and please carry on the enjoyable work.

  17. Muchos Gracias in your weblog post.Thanks Again. Actually Great.

  18. Henry Minute says:

    Just to let you know that your link to the Silverlight Quick Start (http://www.silverlight.net/QuickStarts/BuildUi/CallJavascript.aspx) leads to a “The page you are looking for is not available…” message.

  19. Pingback: Getting ready for Microsoft Silverlight Exam 70-506 (Part 3) | www.nalli.net

  20. Pingback: Getting ready for Microsoft Silverlight Exam 70-506 (Part 3)

Comments are closed.