我有这个班级
public class Tooth
{
public string Id {get;set;}
}
这个custrom控件
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));
}
我的问题是在添加 Tooth依赖属性之后,发生此错误
默认值类型与属性类型
不匹配这个错误究竟是什么意思?设置此DP
答案 0 :(得分:123)
Default value
的{{1}}与您的类型不符。
更改
DP
到
public static readonly DependencyProperty ToothProperty =
DependencyProperty.Register("Tooth", typeof(Tooth), typeof(ToothUI),
new PropertyMetadata(0));
或者只是省略设置DP的默认值:
public static readonly DependencyProperty ToothProperty =
DependencyProperty.Register("Tooth", typeof(Tooth), typeof(ToothUI),
new PropertyMetadata(default(Tooth)));
答案 1 :(得分:3)
我来这里是为了问题的标题,但我的类型是十进制默认值,我用这个0.0M https://msdn.microsoft.com/en-us/library/83fhsxwc.aspx
解决了