PropertyMetadata注册一个类型为“system . windows . point”的Dependency

本文关键字:windows system point Dependency 类型 注册 一个 PropertyMetadata | 更新日期: 2023-09-27 18:18:01

下面是我的代码:

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)
    {
    }
}

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

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

所以我有点确定问题是:

new PropertyMetadata(0)

但据我所知PropertyMetadata应该工作,因为有一个点构造函数,以int为参数:http://msdn.microsoft.com/en-us/library/bf1b4f2b.aspx

所以…

PropertyMetadata注册一个类型为“system . windows . point”的Dependency

是的,你是对的,问题是与传递到PropertyMetada的对象。参数将是你的属性的默认值,所以它必须与你的依赖属性的类型匹配。

在您的情况下,PointValue类型是Point,所以您应该写new PropertyMetadata(new Point(0))

还有您的Description属性,即string: new UIPropertyMetadata("0")new UIPropertyMetadata(""),取决于您的需要。