Tip

The default value of a DependencyProperty is shared by all instances of the class that registers it

Explanation

The last two tips explained how to set the default value of a Silverlight/WPF DependencyProperty. But there's something you need to be aware of when you're using either technique: the default value of a DependencyProperty is shared by all instances of a class. This doesn't tend to matter for value types, immutable reference types, and sharable types (like brushes), but it affects mutable reference types and can lead to unexpected behavior. The most common scenario is creating a collection-type DependencyProperty (for something like Collection(T)) - the intent is for each instance to have its own unique collection, but because the default value is shared, all instances end up sharing the same list! In such cases, there are two things to change: make the DependencyProperty read-only (with RegisterReadOnly) and initialize the property in the class constructor. [Didn't a previous tip say that was bad? Yes, but this scenario is special. :) ] When a class exposes a collection-type DependencyProperty, the intent is typically to use the same collection instance for the life of the object. And that's what makes it okay to set the property in the constructor: it doesn't matter that nobody can override the default value with a Style because they're not supposed to anyway. Next time: Why this can't be done on Silverlight.

Good Example

public Collection<string> MyStringCollection
{
    get { return (Collection<string>)GetValue(MyStringCollectionProperty); }
}
protected static readonly DependencyPropertyKey MyStringCollectionPropertyKey =
    DependencyProperty.RegisterReadOnly(
        "MyStringCollection",
        typeof(Collection<string>),
        typeof(MyControl),
        new PropertyMetadata(null));
public static readonly DependencyProperty MyStringCollectionProperty =
    MyStringCollectionPropertyKey.DependencyProperty;

public MyControl()
{
    SetValue(MyStringCollectionPropertyKey, new Collection<string>());
}

More information