默认值类型不匹配属性的类型 [英] The default value type does not match the type of the property

查看:2127
本文介绍了默认值类型不匹配属性的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个类

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

}



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

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

究竟这个错误是什么意思?什么是设置此 DP

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

推荐答案

<$ C当前方式$ C>默认值为 DP 不符合您的类型。

修改

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



或者干脆省略为您的DP设定的默认值:

Or simply omit setting default value for your DP:

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

这篇关于默认值类型不匹配属性的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆