WPF依赖属性错误

时间:2014-02-13 08:24:18

标签: c# wpf

我正在尝试定义这样的依赖属性:

public static readonly DependencyProperty DependencyPropertyName= DependencyProperty.Register("DepName", typeof(EnumName), typeof(MyWindow1), new FrameworkPropertyMetadata("FrameWorkProperty", FrameworkPropertyMetadataOptions.AffectsRender, Target));

private static void Target(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
    //some logic here
}

public EnumName DepName
{
    get { return (EnumName)GetValue(DependencyPropertyName); }
    set { SetValue(DependencyPropertyName, value); }
}

我得到了这个错误,并且不明白为什么:

{"Default value type does not match type of property 'DepName'."}

1 个答案:

答案 0 :(得分:1)

您的依赖项属性的默认值类型(String)与您的属性DepName(EnumName)的类型不匹配。

更改依赖项属性中的默认类型,它应该可以正常工作。

public static readonly DependencyProperty DependencyPropertyName= DependencyProperty.Register(
    "DepName", 
    typeof(EnumName), 
    typeof(MyWindow1), 
    new FrameworkPropertyMetadata(
        EnumName.SomeValue, // this is the defalt value
        FrameworkPropertyMetadataOptions.AffectsRender, 
        Target));