开发者

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 { S开发者_如何学JAVAetValue(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)
   );


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((short)0));
     }

This seems to work...looks like 0 will be interpreted as int..but why..?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜