Skip to content

Instantly share code, notes, and snippets.

@idiotandrobot
Created July 16, 2014 15:29
Show Gist options
  • Save idiotandrobot/75834c304520a309c4fd to your computer and use it in GitHub Desktop.
Save idiotandrobot/75834c304520a309c4fd to your computer and use it in GitHub Desktop.
MoreObservableCollection
public class MoreObservableCollection<T> : ObservableCollection<T>
{
public MoreObservableCollection()
: base()
{
CollectionChanged += new NotifyCollectionChangedEventHandler(ObservableCollection_CollectionChanged);
}
public MoreObservableCollection(IEnumerable<T> collection)
: base(collection)
{
CollectionChanged += new NotifyCollectionChangedEventHandler(ObservableCollection_CollectionChanged);
}
public MoreObservableCollection(List<T> list)
: base(list)
{
CollectionChanged += new NotifyCollectionChangedEventHandler(ObservableCollection_CollectionChanged);
}
private void ObservableCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (Object item in e.NewItems.OfType<INotifyPropertyChanged>())
{
(item as INotifyPropertyChanged).PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
}
}
if (e.OldItems != null)
{
foreach (Object item in e.OldItems)
{
(item as INotifyPropertyChanged).PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);
}
}
}
void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
NotifyCollectionChangedEventArgs a = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
OnCollectionChanged(a);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment