gpt4 book ai didi

wpf - DependencyProperty默认值

转载 作者:行者123 更新时间:2023-12-04 02:03:35 24 4
gpt4 key购买 nike

我试图让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而在加载窗口时出现空引用异常。

最佳答案

发表评论作为答案。

根据msdn DependencyProperty.Register Method的语法如下:

public static DependencyProperty Register(
string name,
Type propertyType,
Type ownerType,
PropertyMetadata typeMetadata
)

在您的情况下,ownerType是 TescoFoodSummary,propertyType是 Orientation,因此参数具有以下位置:
DependencyProperty.Register("DisplayMode", typeof (Orientation), typeof (TescoFoodSummary), new UIPropertyMetadata(Orientation.Vertical));

关于wpf - DependencyProperty默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5894981/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com