Page view counter

Dependency Properties, Continued

 

First, a hearty thank you to "obsid" who wrote a great comment noting that by using reflector you can find that the documentation is not yet updated and System.Windows.PropertyMetadata's constructor is overloaded. While I am a fan of writing reflective programs (see chapter 20 Programming C# 3.0), for those of you who don't particularly want to dive into this somewhat obscure topic, an easier proof of obsid's point can be obtained in Visual Studio by opening the Object Browser and searching for PropertyMetaData where you will find the following:

  ObjectBrowserPropertyMetaData
(Image modified)

As you may be able to see, there are five overloads: a default constructor, one that takes an object, another that takes an object and a callback, a fourth that takes an object, a callback and a "CoerceValueCallback" and finally a constructor that take a callback alone. Highlighting any of these causes it to be summarized in the associated window in the object browser.

ObjectBrowserSummary

Nifty tool.

A Note on Feedback: I would encourage feedback and corrections, but you are not shy; so keep it coming. As for the specific note about default values, please be assured that issue will be covered when Dependency Property Precedence is covered.

Advantages of Dependency Properties

In the previous discussion, I talked about the "cost" of DPs; specifically the learning curve. The advantages manifestly outweigh the cost and today I'll begin to discuss what you get for this somewhat byzantine system.

The short answer is that Dependency properties were invented to support four forms of "late binding" for properties that are not easily supported when a property is backed by a field:

  • Resources
  • Data binding
  • Styles and Templates
  • Animations

Support for each of these can be accomplished in other ways, of course, but the Dependency Properties system is faster and more efficient than the alternatives.

Note that the Dependency Properties system was not invented for Silverlight, it was originally invented for WPF and then adapted for Silverlight. In fact, some of the breaking changes from Beta2 to RC0 are in service to bringing the Silverlight version more in line with the WPF version.

Resources

Resources are typically specified as child elements of a page root element, or of the application (e.g., in App.xaml)

Data Binding

Data binding works through a specific markup extension syntax in XAML, or the Binding object in code. With data binding, the final property value determination is deferred until run time, at which time the value is obtained from a data source.

Styles and Templates

Any property of a control can be changed from its declared appearance by the use of a Style or Template and this may be changed dynamically and at run time.

Animation

Animation is implemented through Storyboards where the target of the animation is the property of a control. The value of the property must be able to change rapidly and very frequently.

All of this would be accomplished in traditional .NET through reflection, but reflection is a notoriously slow process, not nearly fast enough for data binding, let alone animation.

Attached Properties

One particular, common and powerful use of dependency properties is the creation of attached properties --  the illusion of a property in an object that is actually a property of another (typically containing) object.  Thus, when a Textblock is declared as

<Textblock text="Who put me here?" grid.Column="1" Grid.Row="0" x:Name="myTB" />

The properties Grid.Column and Grid.Row are not properties of the TextBlock but are attached to the TextBlock through the use of a form of Dependency Properties called Attached Properties.

Precedence

One of the aspects of Dependency properties that make them very valuable in data binding and animation scenarios is that they have a very well defined precedence for how their value is determined when more than one potential source affects them (that is, a property might potentially set by data binding, a style and animation; which one actually sets the property?)

That is the topic for the next installment.

Thanks!

-jesse

Published Wednesday, October 01, 2008 2:03 PM by jesseliberty

Comments

# Silverlight news for October 2, 2008

Pingback from  Silverlight news for October 2, 2008

Thursday, October 02, 2008 2:04 AM by Silverlight news for October 2, 2008

# 2008 October 02 - Links for today &laquo; My (almost) Daily Links

Pingback from  2008 October 02 - Links for today &laquo; My (almost) Daily Links

Thursday, October 02, 2008 3:59 AM by 2008 October 02 - Links for today « My (almost) Daily Links

# Dew Drop - October 2, 2008 | Alvin Ashcraft's Morning Dew

Pingback from  Dew Drop - October 2, 2008 | Alvin Ashcraft's Morning Dew

Thursday, October 02, 2008 8:48 AM by Dew Drop - October 2, 2008 | Alvin Ashcraft's Morning Dew

# Custom Controls – The Denouement

Over the past month I've posted half a dozen min-articles about creating custom controls that can interact

Thursday, October 09, 2008 9:48 AM by Jesse Liberty - Silverlight Geek

# Custom Controls – The Dénouement

Over the past month I've posted half a dozen min-articles about creating custom controls that can interact

Thursday, October 09, 2008 10:10 AM by Microsoft Weblogs

# Custom Controls – The Dénouement

Over the past month I&#39;ve posted half a dozen min-articles about creating custom controls that can

Thursday, October 09, 2008 10:38 AM by Mirrored Blogs

# Custom Controls - the handling at Blog von J??rgen Ebner

Pingback from  Custom Controls - the handling at Blog von J??rgen Ebner

Friday, October 10, 2008 6:15 AM by Custom Controls - the handling at Blog von J??rgen Ebner

# Creating Skinnable Custom Controls - Video

&#160; &#160; I have completed the four part video series Creating Skinnable Customer Controls and I

Friday, December 05, 2008 3:01 PM by Jesse Liberty - Silverlight Geek

# Creating Skinnable Custom Controls - Video

&#160; &#160; I have completed the four part video series Creating Skinnable Customer Controls and I

Friday, December 05, 2008 3:52 PM by Microsoft Weblogs

# Erstellen von anpassbaren Custom Control at Programming with Silverlight, WPF &amp; .NET

Pingback from  Erstellen von anpassbaren Custom Control at Programming with Silverlight, WPF &amp; .NET

# Application Set: Templates & Custom Controls

When we started creating How Do I videos, the idea was to have stand alone videos that do not depend

Monday, December 22, 2008 5:34 PM by Jesse Liberty - Silverlight Geek

# Application Set: Templates & Custom Controls

When we started creating How Do I videos, the idea was to have stand alone videos that do not depend

Monday, December 22, 2008 6:17 PM by Microsoft Weblogs

# Templates &#038; Custum Controls at Programming with Silverlight, WPF &amp; .NET

Pingback from  Templates &#038; Custum Controls at Programming with Silverlight, WPF &amp; .NET

# Templates &#038; Custum Controls at Programming with Silverlight, WPF &amp; .NET

Pingback from  Templates &#038; Custum Controls at Programming with Silverlight, WPF &amp; .NET

# What’s New In Silverlight 3

&#160; &#160; I had the pleasure of presenting What’s New In Silverlight 3 both at TechEd this year and

Friday, May 29, 2009 4:57 PM by Jesse Liberty - Silverlight Geek

# What’s New In Silverlight 3

I had the pleasure of presenting What’s New In Silverlight 3 both at TechEd this year and then again

Friday, May 29, 2009 5:42 PM by Microsoft Weblogs

# Programming with Silverlight, WPF &amp; .NET &raquo; Was ist neu in Silverlight 3

Pingback from  Programming with Silverlight, WPF &amp; .NET &raquo; Was ist neu in Silverlight 3

# Top-silverlight &raquo; Blog Archive &raquo; What???s New In Silverlight 3

Pingback from  Top-silverlight  &raquo; Blog Archive   &raquo; What???s New In Silverlight 3