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 is Pointso と書く必要がありますnew PropertyMetadata(new Point(0))

また、あなたのニーズに応じて、あなたのDescription財産stringです 。new UIPropertyMetadata("0")new UIPropertyMetadata("")

于 2011-12-15T11:13:31.320 に答える