Ech*_*lon 11 wpf binding dependency-properties default-value
我正在尝试在WPF中使用DependencyProperty.我正在使用:
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); }
}
Run Code Online (Sandbox Code Playgroud)
当我初始化窗口时,我收到一个错误:默认值类型与属性'DisplayMode'的类型不匹配.但是,如果我保留默认值,则由于未设置DisplayModeProperty而导致窗口加载时,我会得到一个空引用异常.
vor*_*olf 13
发表评论作为答案.
根据msdn DependencyProperty.Register方法,语法看起来如此:
public static DependencyProperty Register(
string name,
Type propertyType,
Type ownerType,
PropertyMetadata typeMetadata
)
Run Code Online (Sandbox Code Playgroud)
在您的情况下,ownerType为TescoFoodSummary和propertyType为Orientation,因此参数具有以下位置:
DependencyProperty.Register("DisplayMode", typeof (Orientation), typeof (TescoFoodSummary), new UIPropertyMetadata(Orientation.Vertical));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12677 次 |
| 最近记录: |