DependencyProperty Default Value

Echilon picture Echilon · May 5, 2011 · Viewed 13.3k times · Source

I'm trying to get a DependencyProperty working in WPF. I'm using:

public static readonly DependencyProperty DisplayModeProperty = DependencyProperty.Register("DisplayMode", typeof (TescoFoodSummary), typeof (Orientation), new UIPropertyMetadata(Orientation.Vertical));
    /// <summary>
    /// Gets or sets the orientation.
    /// </summary>
    /// <value>The orientation.</value>
    public Orientation DisplayMode {
        get { return (Orientation)base.GetValue(DisplayModeProperty); }
        set { base.SetValue(DisplayModeProperty, value); }
    }

When I initialize the window, I get an error: Default value type does not match type of property 'DisplayMode'. Howevere, if I leave the default value out I get a null reference exception when the window loads due to DisplayModeProperty not being set.

Answer

vortexwolf picture vortexwolf · Aug 27, 2012

Posting comment as an answer.

According to msdn DependencyProperty.Register Method the syntax looks so:

public static DependencyProperty Register(
    string name,
    Type propertyType,
    Type ownerType,
    PropertyMetadata typeMetadata
)

In your case ownerType is TescoFoodSummary and propertyType is Orientation, so parameters have the following positions:

DependencyProperty.Register("DisplayMode", typeof (Orientation), typeof (TescoFoodSummary), new UIPropertyMetadata(Orientation.Vertical));