public static void SubscribeCollectionItems <T>(this SubscribableCollection <T> collection, Action <T, PropertyChangedEventArgs> handler) where T : INotifyPropertyChanged { void itemsHandler(object item, PropertyChangedEventArgs e) { handler((T)item, e); } void collectionHandler(object sender, NotifyCollectionChangedEventArgs e) { if (e.Action == NotifyCollectionChangedAction.Add || e.Action == NotifyCollectionChangedAction.Remove) { if (e.NewItems != null) { foreach (T item in e.NewItems) { item.PropertyChanged += itemsHandler; } } if (e.OldItems != null) { foreach (T item in e.OldItems) { item.PropertyChanged -= itemsHandler; } } } } collection.CollectionChanged += collectionHandler; }
protected virtual bool RefillCollection <T>(ref SubscribableCollection <T> storage, IEnumerable <T> value, [CallerMemberName] string propertyName = "") { if (storage == value || value == null) { return(false); } else { OnPropertyChanging(propertyName); if (storage == null) { if (value is SubscribableCollection <T> initCollection) { storage = initCollection; } else { storage = new SubscribableCollection <T>(); storage.AddRange(value); } } else { storage.ReplaceWhole(value); } OnPropertyChanged(propertyName); return(true); } }