DependencyProperty默认值
我正在尝试让 DependencyProperty 在 WPF 中工作。我正在使用:
public static readonly DependencyProperty DisplayModeProperty = DependencyProperty.Register("DisplayMode", typeof (TescoFoodSummary), typeof (Orientation), new UIPropertyMetadata(Orientation.Vertical));
/// <summary>
/// Gets or sets the orientation.
/// </summary>
/// <value>The orientation.</value>
public Orientation DisplayMode {
get { return (Orientation)base.GetValue(DisplayModeProperty); }
set { base.SetValue(DisplayModeProperty, value); }
}
当我初始化窗口时,出现错误:默认值类型与属性“DisplayMode”的类型不匹配。但是,如果我保留默认值,则在加载窗口时,由于未设置 DisplayModeProperty,我会收到空引用异常。
I'm trying to get a DependencyProperty working in WPF. I'm using:
public static readonly DependencyProperty DisplayModeProperty = DependencyProperty.Register("DisplayMode", typeof (TescoFoodSummary), typeof (Orientation), new UIPropertyMetadata(Orientation.Vertical));
/// <summary>
/// Gets or sets the orientation.
/// </summary>
/// <value>The orientation.</value>
public Orientation DisplayMode {
get { return (Orientation)base.GetValue(DisplayModeProperty); }
set { base.SetValue(DisplayModeProperty, value); }
}
When I initialize the window, I get an error: Default value type does not match type of property 'DisplayMode'. Howevere, if I leave the default value out I get a null reference exception when the window loads due to DisplayModeProperty not being set.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发表评论作为答案。
根据msdn DependencyProperty.Register Method,语法如下所示:
在您的情况下ownerType 为
TescoFoodSummary
,propertyType 为Orientation
,因此参数具有以下位置:Posting comment as an answer.
According to msdn DependencyProperty.Register Method the syntax looks so:
In your case ownerType is
TescoFoodSummary
and propertyType isOrientation
, so parameters have the following positions: