Juan Pablo Gomez
Juan Pablo Gomez

Reputation: 5534

The default value type does not match the type of the property

I have this class

public class Tooth
{
    public string Id {get;set;}
}

And this custrom control

public partial class ToothUI : UserControl
{
    public ToothUI()
    {
        InitializeComponent();
    }

    public Tooth Tooth
    {
        get { return (Tooth)GetValue(ToothProperty); }
        set
        {
            SetValue(ToothProperty, value);
            NombrePieza.Text =   value.Id.Replace("_",String.Empty);
        }
    }
    public static readonly DependencyProperty ToothProperty =
        DependencyProperty.Register("Tooth", typeof(Tooth), typeof(ToothUI), new PropertyMetadata(0)); 

}

My problem is after Add Tooth dependency property, this error happen

The default value type does not match the type of the property

What exactly this error mean? What is the current way to set this DP

Upvotes: 99

Views: 33647

Answers (2)

dpineda
dpineda

Reputation: 2448

I came here for the title of the question but my type was a decimal default value and I solved with this 0.0M.

Refer to Default values of C# types.

Upvotes: 8

Rohit Vats
Rohit Vats

Reputation: 81253

Default value for DP does not match your type.

Change

public static readonly DependencyProperty ToothProperty =
        DependencyProperty.Register("Tooth", typeof(Tooth), typeof(ToothUI),
                                         new PropertyMetadata(0));

to

public static readonly DependencyProperty ToothProperty =
        DependencyProperty.Register("Tooth", typeof(Tooth), typeof(ToothUI),
                                      new PropertyMetadata(default(Tooth)));

Or simply omit setting default value for your DP:

public static readonly DependencyProperty ToothProperty =
        DependencyProperty.Register("Tooth", typeof(Tooth), typeof(ToothUI));

Upvotes: 194

Related Questions