bij*_*iju 4 wpf dependency-properties default-value
我试图在我的代码中使用tis depencency属性,但它给我错误说默认值类型与属性'MyProperty'的类型不匹配.但是short应该接受0作为默认值.
如果我尝试将其作为默认值赋予它,则它可以工作,即使它是非nullabel类型.怎么会发生这种情况..
public short MyProperty
{
get { return (short)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}
Run Code Online (Sandbox Code Playgroud)
使用DependencyProperty作为MyProperty的后备存储.这可以实现动画,样式,装订等......
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.Register(
"MyProperty",
typeof(short),
typeof(Window2),
new UIPropertyMetadata(0)
);
Run Code Online (Sandbox Code Playgroud)
Abe*_*cht 13
问题是C#编译器将文字值解释为整数.您可以告诉它将它们解析为long或ulongs(40L是长的,40UL是ulong),但是没有简单的方法来声明short.
简单地转换文字将起作用:
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)
);
Run Code Online (Sandbox Code Playgroud)