Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wpf DependencyProperty Not accepting default value for short

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)
    );
like image 780
biju Avatar asked Dec 12 '22 22:12

biju


1 Answers

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)
   );
like image 156
Abe Heidebrecht Avatar answered Dec 16 '22 17:12

Abe Heidebrecht