在这个实际例子中使用wpf中的依赖属性

时间:2013-12-19 10:54:51

标签: .net wpf

我想使用将代表ICarRepository实例的依赖项属性。

所以我找到this post并根据acc。回答我尝试了我的例子

 public static readonly DependencyProperty RepositoryProperty = DependencyProperty.Register(
    null,
    typeof(ICarRepository ),
    typeof(TreassureControl),
    **new PropertyMetadata(??????)** what to put here?
    );

    public ICarRepository Repository
    {
       get { return (ICarRepository)GetValue(RepositoryProperty); }
       set { SetValue(RepositoryProperty, value); }
    }

1 个答案:

答案 0 :(得分:2)

PropertyIdentifier不能为null(第一个参数),在其中传递CLR包装器属性名称Repository

关于PropertyMetadata,您可以将其设置为null。此外,您可以在不需要传递该值的情况下使用其他重载。

public static readonly DependencyProperty RepositoryProperty = 
 DependencyProperty.Register(
       "Repository",
        typeof(ICarRepository ),
        typeof(TreassureControl),
        new PropertyMetadata(null));

或者只是避免使用上一个参数:

public static readonly DependencyProperty RepositoryProperty = 
 DependencyProperty.Register(
       "Repository",
        typeof(ICarRepository ),
        typeof(TreassureControl));