I was trying to use tis depencency property in my code but it gives me error says that Default value type does not match type of property 'MyProperty'. But short should accept 0 as default value.
If I try to give it a null as default value it works, even if its a non nullabel type. How come this happens..
public short MyProperty
{
get { return (short)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}
Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.Register(
"MyProperty",
typeof(short),
typeof(Window2),
new UIPropertyMetadata(0)
);
The problem is that the C# compiler interprets literal values as integers. You can tell it to parse them as longs or ulongs (40L is a long, 40UL is ulong), but there isn't an easy way to declare a short.
Simply casting the literal will work:
public short MyProperty
{
get { return (short)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.Register(
"MyProperty",
typeof(short),
typeof(Window2),
new UIPropertyMetadata((short)0)
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With