Skip to content

Instantly share code, notes, and snippets.

@itajaja
Created November 17, 2013 00:04
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itajaja/7507120 to your computer and use it in GitHub Desktop.
Save itajaja/7507120 to your computer and use it in GitHub Desktop.
TrulyObservableCollection fires collectionchanged also when items change
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
namespace Hylasoft.OrdersGui.Utils
{
public class TrulyObservableCollection<T> : ObservableCollection<T>
where T : INotifyPropertyChanged
{
public TrulyObservableCollection()
{
CollectionChanged += TrulyObservableCollection_CollectionChanged;
}
void TrulyObservableCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (Object item in e.NewItems)
{
(item as INotifyPropertyChanged).PropertyChanged += item_PropertyChanged;
}
}
if (e.OldItems != null)
{
foreach (Object item in e.OldItems)
{
(item as INotifyPropertyChanged).PropertyChanged -= item_PropertyChanged;
}
}
}
void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
var a = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
OnCollectionChanged(a);
}
}
}
@lgaudouen
Copy link

Do you have the latest version of this excellent source?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment