Wpf DependencyProperty Don't accept default value for short

I tried to use the tis depencency property in my code, but it gives me an error saying that the default value type does not match the "MyProperty" property type. But short should be set to 0 by default.

If I try to set it to null as the default value, it will work even if it is not null. How did it happen.

public short MyProperty { get { return (short)GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } } 

Using DependencyProperty as storage for MyProperty. This allows you to create animation, style, snapping, etc.

 public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register( "MyProperty", typeof(short), typeof(Window2), new UIPropertyMetadata(0) ); 
+4
source share
2 answers

The problem is that the C # compiler interprets literal values โ€‹โ€‹as integers. You can say this to parse them as longs or ulongs (40L - long, 40UL - ulong), but there is no easy way to declare short.

Simple literal casting 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) ); 
+13
source
 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)); } 

It seems that this works ... it looks like 0 will be interpreted as int .. but why ..?

0
source

Source: https://habr.com/ru/post/1307141/


All Articles