默认值类型与属性的类型不匹配

我有课

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

还有这个定制控件

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));


}

我的问题是在添加 牙齿依赖属性之后,会发生这个错误

默认值类型与属性的类型不匹配

这个错误到底是什么意思? 当前设置这个 DP的方法是什么

30224 次浏览

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));

I came here for the title of the question but my type was a decimal default value and i solved with this 0.0M https://msdn.microsoft.com/en-us/library/83fhsxwc.aspx