Files
BeWoPlaner/Shared/Core/TrulyObservableCollection.cs
2016-06-27 01:45:38 +02:00

48 lines
1.3 KiB
C#

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using BS.Shared.Extensions;
namespace BS.Shared.Core
{
public sealed class TrulyObservableCollection<T> : ObservableCollection<T> where T : INotifyPropertyChanged
{
public TrulyObservableCollection()
{
CollectionChanged += TrulyObservableCollection_CollectionChanged;
}
public TrulyObservableCollection(IEnumerable<T> collection)
{
CollectionChanged += TrulyObservableCollection_CollectionChanged;
this.AddRange(collection);
}
void TrulyObservableCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (var notifyPropertyChanged in e.NewItems.OfType<INotifyPropertyChanged>())
{
notifyPropertyChanged.PropertyChanged += item_PropertyChanged;
}
}
if (e.OldItems == null)
return;
foreach (var notifyPropertyChanged in e.OldItems.OfType<INotifyPropertyChanged>())
{
notifyPropertyChanged.PropertyChanged -= item_PropertyChanged;
}
}
void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
var a = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
OnCollectionChanged(a);
}
}
}