Reputation: 6124
I have a class that inherits the base Collection
class (ViewModel for a dataGridRow)
now, I would like to add a DependencyProperty
to this class so that I can easily bind to it. problem is: Collection
is not a DependencyObject
so I cannot use the GetValue()
and SetValue()
methods, and C# doesn't do multiple inheritance so that I can inherit Collection
as well as DependencyObject
.
is there an easy solution for this?
or do I have no choice but to resort to a simple Property
, inherit INotifyPropertyChanged
and implement PropertyChanged
?
Upvotes: 0
Views: 205
Reputation: 6124
just for the sake of closing this question, I'll settle for: "no, this isn't possible".
alex's answer offers an interesting perspective, but in my case and as stated in my comment, this would not make anything easier or more readable.
in the end, I implemented INPC
Upvotes: 0
Reputation: 7591
IMHO ViewModel should never ever implement DependencyObject, but instead implement INotifyPropertyChanged(INPC).
DataBinding to Dependency Properties is indeed faster than Binding to INPC, since no reflection is involved, but unless you're dealing with sh*tloads of data, this won't be an issue.
Implemting DependencyObject is strictly for UI elements, not for anything else, and the infrastructure that comes with DP is a lot more than just change notification. ViewModel classes are not UI oriented by definition, and hence inheriting DependencyObject is a design smell.
Upvotes: 2
Reputation: 3397
Use aggregation instead of multi-inheritance:
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
}
Upvotes: 0