03.01.2017

WPF: Error- "Default value type does not match type of property..."

At last I have my holidays and some spare time to write more posts to my blog. :)

When trying to add dependency property of type double you can easily get the error "Default value type does not match type of property..." with such code:
public double SomeDoubleValue
{
    get { return (double)GetValue(SomeDoubleValueProperty); }
    set { SetValue(SomeDoubleValueProperty, value); }
}
 
public static readonly DependencyProperty SomeDoubleValueProperty=
    DependencyProperty.Register("SomeDoubleValue", typeof(double),
    typeof(ownerclass), new PropertyMetadata(0));

The problem here is that "0" is not casted as double, that is why you should cast it explicitly like
public static readonly DependencyProperty SomeDoubleValueProperty=
    DependencyProperty.Register("SomeDoubleValue", typeof(double), 
    typeof(ownerclass), new PropertyMetadata(0D));

Some more information can be found here.

Комментариев нет:

Отправить комментарий