我试图在我的代码中使用这个依赖属性,但它给了我一个错误,说默认值类型与属性 'MyProperty' 的类型不匹配。 但是 short 应该接受 0 作为默认值。
如果我尝试将其设置为 null 作为默认值,则可以工作,即使它是非空类型。 这是怎么发生的..
public short MyProperty
{
get { return (short)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}
将一个DependencyProperty作为MyProperty的后备存储。这样可以实现动画、样式、绑定等功能...
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.Register(
"MyProperty",
typeof(short),
typeof(Window2),
new UIPropertyMetadata(0)
);