Hallo All
I have this
ObservableCollectionEx
class.
Basically, all I want from it is to extend the
INotifyPropertyChanged
event to any changes on any member of the collection.
ObservableCollectionEx Class
public partial class ObservableCollectionEx<T> : ObservableCollection<T>
where T : INotifyPropertyChanged
{
public ObservableCollectionEx() : base() { }
public ObservableCollectionEx(List<T> list)
: base((list != null) ? new List<T>(list.Count) : list)
{
CopyFrom(list);
}
public ObservableCollectionEx(IEnumerable<T> collection)
{
if (collection == null)
throw new ArgumentNullException("collection");
CopyFrom(collection);
}
private void CopyFrom(IEnumerable<T> collection)
{
IList<T> items = Items;
if (collection != null && items != null)
{
using (IEnumerator<T> enumerator = collection.GetEnumerator())
{
while (enumerator.MoveNext())
{
items.Add(enumerator.Current);
}
}
}
}
protected override void InsertItem(int index, T item)
{
base.InsertItem(index, item);
item.PropertyChanged += Item_PropertyChanged;
}
protected override void RemoveItem(int index)
{
Items[index].PropertyChanged -= Item_PropertyChanged;
base.RemoveItem(index);
}
protected virtual void MoveItem(int oldIndex, int newIndex)
{
T removedItem = this[oldIndex];
base.RemoveItem(oldIndex);
base.InsertItem(newIndex, removedItem);
}
protected override void ClearItems()
{
foreach (T item in Items)
{
item.PropertyChanged -= Item_PropertyChanged;
}
base.ClearItems();
}
protected override void SetItem(int index, T item)
{
T oldItem = Items[index];
T newItem = item;
oldItem.PropertyChanged -= Item_PropertyChanged;
newItem.PropertyChanged += Item_PropertyChanged;
base.SetItem(index, item);
}
private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
var handler = ItemPropertyChanged;
if (handler != null) { handler(sender, e); }
}
public event PropertyChangedEventHandler ItemPropertyChanged;
}
Additionally I have an extension to the
ObservableCollection
class, a simple
ToObservableCollection()
method.
CollectionExtensions - ToObservableCollection()
public static class CollectionExtensions
{
public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> enumerableList)
{
return enumerableList != null ? new ObservableCollection<T>(enumerableList) : null;
}
}
As I needed the ToObservableCollectionEx(), I build it the same way I did to the ToObservableCollection.
public static ObservableCollectionEx<T> ToObservableCollectionEx<T>(this IEnumerable<T> enumerableList)
{
return enumerableList != null ? new ObservableCollectionEx<T>(enumerableList) : null;
}
But I get this error:
"
The type 'T' cannot be used as type parameter 'T' in the generic type or method 'Portal_Tools.ObservableCollectionEx<t>'. There is no boxing conversion or type parameter conversion from 'T' to 'System.ComponentModel.INotifyPropertyChanged'."
Googled around this message and couldn't find any answer that would solve this error.
Tried different approaches but always got the same error because the <T> is always there.
Can anyone help me out and give me a clue on how to solve this error?
Thanks