2011-03-24 97 views
0

我有继承的基础Collection类(视图模型的dataGridRow)一类将一个dependencyProperty添加到集合?

现在,我想一个DependencyProperty添加到这个类,这样我可以轻松地绑定到它。问题是:Collection不是DependencyObject,所以我不能使用GetValue()SetValue()方法,并且C#不会执行多重继承,因此我可以继承Collection以及DependencyObject

有没有简单的解决方案呢?

还是我别无选择,只能求助于简单的Property,继承INotifyPropertyChanged并实现PropertyChanged

回答

0

只是为了解决这个问题,我会解决:“不,这是不可能的”。

亚历克斯的答案提供了一个有趣的观点,但在我的情况和正如我的评论所述,这不会使任何事情更容易或更具可读性。

到底

,我实现INPC

0

使用聚合而不是多继承:

class MyCollection<T> : DependencyObject, ICollection<T> 
{ 
    // Inner collection for call redirections. 
    private Collection<T> _collection; 

    #region ICollection<T> Members 

    public void Add(T item) 
    { 
     this._collection.Add(item); 
    } 

    public void Clear() 
    { 
     this._collection.Add(clear); 
    } 

    // Other ICollection methods ... 
    #endregion 

    #region MyProperty Dependency Property 

    public int MyProperty 
    { 
     get 
     { 
      return (int)this.GetValue(MyCollection<T>.MyPropertyProperty); 
     } 
     set 
     { 
      this.SetValue(MyCollection<T>.MyPropertyProperty, value); 
     } 
    } 

    public static readonly DependencyProperty MyPropertyProperty = 
     DependencyProperty.Register("MyProperty", 
      typeof(int), 
      typeof(MyCollection<T>), 
      new FrameworkPropertyMetadata(0)); 

    #endregion 
} 
+0

这确实有可能使用DP,但其编写的ICollection的方法那种取消不必编写的INPC的优势 – David 2011-03-24 13:18:54

2

恕我直言视图模型应该永远实现的DependencyObject,而是执行INotifyPropertyChanged(INPC)。

DataBinding依赖属性确实比绑定到INPC更快,因为没有涉及反射,但除非你处理sh * tloads数据,这不会是一个问题。

实现DependencyObject严格适用于UI元素,而不是其他任何东西,而DP附带的基础结构不仅仅是更改通知。 ViewModel类不是按照定义面向UI的,因此继承DependencyObject是一种设计风味。

相关问题