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); } }
private void ModelsCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (e.Action == NotifyCollectionChangedAction.Reset) { internalList.Clear(); foreach (var model in modelsCollection) { internalList.Add(VMFabric(model)); } } else if (e.Action == NotifyCollectionChangedAction.Add) { if (e.NewStartingIndex > -1) { foreach (ModelType model in e.NewItems.Cast <ModelType>().Reverse()) { internalList.Insert(e.NewStartingIndex, VMFabric(model)); } } else { internalList.AddRange(e.NewItems.Cast <ModelType>().Select(model => VMFabric(model))); } } else if (e.Action == NotifyCollectionChangedAction.Move) { internalList.Move(e.OldStartingIndex, e.NewStartingIndex); } else if (e.Action == NotifyCollectionChangedAction.Remove) { var removedArray = e.OldItems.Cast <ModelType>().ToArray(); internalList.RemoveAll(x => removedArray.Contains(x.Model)); } else if (e.Action == NotifyCollectionChangedAction.Replace) { internalList[e.OldStartingIndex] = VMFabric((ModelType)e.NewItems[0]); } }