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


82

我有这堂课

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

Answers:


160

Default value对于DP不符合您的类型。

更改

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

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

或者,只需省略DP的默认设置即可:

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

2
非常感谢您的帮助
Juan Pablo Gomez 2013年

1
很高兴能帮助Juan .. :)
Rohit Vats 2013年

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.