diff --git a/MvvmHelpers/ObservableRangeCollection.cs b/MvvmHelpers/ObservableRangeCollection.cs index f4433c5..40c8902 100644 --- a/MvvmHelpers/ObservableRangeCollection.cs +++ b/MvvmHelpers/ObservableRangeCollection.cs @@ -54,11 +54,7 @@ public void AddRange(IEnumerable collection, NotifyCollectionChangedAction no } if (raiseEvents) - { - OnPropertyChanged(new PropertyChangedEventArgs("Count")); - OnPropertyChanged(new PropertyChangedEventArgs("Item[]")); - OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); - } + RaiseChangeNotificationEvents(action: NotifyCollectionChangedAction.Reset); return; } @@ -70,10 +66,11 @@ public void AddRange(IEnumerable collection, NotifyCollectionChangedAction no if (changedItems.Count == 0) return; - - OnPropertyChanged(new PropertyChangedEventArgs("Count")); - OnPropertyChanged(new PropertyChangedEventArgs("Item[]")); - OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, changedItems, startIndex)); + + RaiseChangeNotificationEvents( + action: NotifyCollectionChangedAction.Add, + changedItems: changedItems, + startingIndex: startIndex); } /// @@ -98,7 +95,7 @@ public void RemoveRange(IEnumerable collection, NotifyCollectionChangedAction } if (raiseEvents) - OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); + RaiseChangeNotificationEvents(action: NotifyCollectionChangedAction.Reset); return; } @@ -116,9 +113,9 @@ public void RemoveRange(IEnumerable collection, NotifyCollectionChangedAction if (changedItems.Count == 0) return; - OnPropertyChanged(new PropertyChangedEventArgs("Count")); - OnPropertyChanged(new PropertyChangedEventArgs("Item[]")); - OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, changedItems, -1)); + RaiseChangeNotificationEvents( + action: NotifyCollectionChangedAction.Remove, + changedItems: changedItems); } /// @@ -138,6 +135,15 @@ public void ReplaceRange(IEnumerable collection) AddRange(collection, NotifyCollectionChangedAction.Reset); } + private void RaiseChangeNotificationEvents(NotifyCollectionChangedAction action, List changedItems = null, int startingIndex = -1) + { + OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count))); + OnPropertyChanged(new PropertyChangedEventArgs("Item[]")); + + if (changedItems is null) + OnCollectionChanged(new NotifyCollectionChangedEventArgs(action)); + else + OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, changedItems: changedItems, startingIndex: startingIndex)); + } } } -