Here is my code :
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)
{
}
}
It compiles but when I want to CreateInstance of my type it crashes with the following error :
{"Default value type does not match type of property 'PointValue'."}
So Im kinda sure the problem is :
new PropertyMetadata(0)
but as far as I understand PropertyMetadata it should work as there is a Point constructor that takes an int as parameter : http://msdn.microsoft.com/en-us/library/bf1b4f2b.aspx
So… what is wrong ?
Yes you are right the problem is with the object passed into
PropertyMetada.The parameter will be the default value for your property so it has to match with the type of your dependency property.In your case
PointValuetype isPointso you should writenew PropertyMetadata(new Point(0)).Also your
Descriptionproperty which isstring:new UIPropertyMetadata("0")ornew UIPropertyMetadata("")depending on your needs.