2011-12-15 8 views
1

が私のコードです:"System.Windows.Point"タイプのDependencyPropertyを登録するPropertyMetadata?ここで

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

だから何が間違っていますか?

答えて

3

はい、問題はオブジェクトがPropertyMetadaに渡されていることです。このパラメータはプロパティのデフォルト値になるため、依存関係プロパティのタイプと一致する必要があります。

PointValueタイプはPointですので、new PropertyMetadata(new Point(0))と書いてください。

また、:new UIPropertyMetadata("0")またはnew UIPropertyMetadata("")というお客様のニーズに応じて、Descriptionプロパティもあります。

+0

新しいPropertyMetadata(新しいPoint(0))を試しましたが、クラッシュして戻ってきました。実際には、文字列のためにクラッシュしていた、そのような時間の無駄... :( –

関連する問題