1

这是我的代码:

public class ExoticPoint : DependencyObject
{
    public Point PointValue
    {
        get { return (Point)GetValue(PointValueProperty); }
        set { SetValue(PointValueProperty, value); }
    }

    public static readonly DependencyProperty PointValueProperty =
        DependencyProperty.Register("PointValue", typeof(Point), typeof(ExoticPoint), new PropertyMetadata(0));



    public string Description
    {
        get { return (string)GetValue(DescriptionProperty); }
        set { SetValue(DescriptionProperty, value); }
    }

    public static readonly DependencyProperty DescriptionProperty =
        DependencyProperty.Register("Description", typeof(string), typeof(ExoticPoint), new UIPropertyMetadata(0));

    public ExoticPoint()
    {
    }

    public ExoticPoint (string objectName)
        : base(objectName)
    {
    }
}

它可以编译,但是当我想创建我的类型的 CreateInstance 时,它​​会崩溃并出现以下错误:

{"Default value type does not match type of property 'PointValue'."}

所以我有点确定问题是:

new PropertyMetadata(0)

但据我了解 PropertyMetadata 它应该可以工作,因为有一个以 int 作为参数的 Point 构造函数:http: //msdn.microsoft.com/en-us/library/bf1b4f2b.aspx

所以……怎么了?

4

1 回答 1

3

是的,您是对的,问题在于传递给 . 的对象PropertyMetada。参数将是您的属性的默认值,因此它必须与您的依赖属性的类型相匹配。

在你的情况下,你应该写PointValuetype 。 Pointnew PropertyMetadata(new Point(0))

还有您的Description财产stringnew UIPropertyMetadata("0")new UIPropertyMetadata("")取决于您的需要。

于 2011-12-15T11:13:31.320 回答