|  | 
| 1 | 1 | # WpfObservableRangeCollection | 
| 2 | 2 | 
 | 
|  | 3 | +[](https://www.nuget.org/packages/WpfObservableRangeCollection/) | 
|  | 4 | +[](https://github.com/CodingOctocat/WpfObservableRangeCollection) | 
|  | 5 | +[](https://github.com/CodingOctocat/WpfObservableRangeCollection/issues) | 
|  | 6 | +[](https://github.com/CodingOctocat/WpfObservableRangeCollection/stargazers) | 
|  | 7 | +[](https://github.com/CodingOctocat/WpfObservableRangeCollection/blob/master/LICENSE) | 
|  | 8 | +[](https://www.codefactor.io/repository/github/codingoctocat/wpfobservablerangecollection) | 
|  | 9 | + | 
| 3 | 10 | Provides ObservableRangeCollection and its WPF version, including AddRange, InsertRange, RemoveRange/RemoveAll, Replace/ReplaceRange methods for bulk operation to avoid frequent update notification events. | 
| 4 | 11 | 
 | 
| 5 |  | ---- | 
|  | 12 | +## NuGet Package Manager | 
| 6 | 13 | 
 | 
| 7 |  | -[](https://www.nuget.org/packages/WpfObservableRangeCollection/) | 
|  | 14 | +    PM> Install-Package WpfObservableRangeCollection | 
|  | 15 | + | 
|  | 16 | +## .NET CLI | 
|  | 17 | + | 
|  | 18 | +    dotnet add package WpfObservableRangeCollection | 
| 8 | 19 | 
 | 
| 9 | 20 | --- | 
| 10 | 21 | 
 | 
| 11 | 22 | ## Classes | 
| 12 |  | -- `ObservableRangeCollection`: Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed. | 
|  | 23 | +- `ObservableRangeCollection`: An ObservableCollection that supports bulk operations to avoid frequent update notification events. | 
| 13 | 24 |   > Forked from [weitzhandler/rangeobservablecollection-cs](https://gist.github.com/weitzhandler/65ac9113e31d12e697cb58cd92601091#file-rangeobservablecollection-cs) | 
| 14 | 25 | 
 | 
| 15 |  | -- `WpfObservableRangeCollection`: Wpf version of ObservableRangeCollection with CollectionView support. | 
|  | 26 | +- `WpfObservableRangeCollection`: WPF version of ObservableRangeCollection with CollectionView support. | 
| 16 | 27 |   > Forked from [weitzhandler/wpfobservablerangecollection-cs](https://gist.github.com/weitzhandler/65ac9113e31d12e697cb58cd92601091#file-wpfobservablerangecollection-cs) | 
| 17 | 28 | 
 | 
|  | 29 | +# Usage | 
|  | 30 | + | 
|  | 31 | +```csharp | 
|  | 32 | +var collection = new WpfObservableRangeCollection<int>(); | 
|  | 33 | +collection.AddRange(Enumerable.Range(0,10)); | 
|  | 34 | +``` | 
|  | 35 | +> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } | 
|  | 36 | +
 | 
|  | 37 | +</br> | 
|  | 38 | + | 
|  | 39 | +```csharp | 
|  | 40 | +collection.RemoveRange(index: 5, count: 3); | 
|  | 41 | +``` | 
|  | 42 | +> { 0, 1, 2, 3, 4, ~~5, 6, 7~~ 8, 9 } | 
|  | 43 | +
 | 
|  | 44 | +</br> | 
|  | 45 | + | 
|  | 46 | +```csharp | 
|  | 47 | +// You can also receive the return value to get the number of items that were successfully removed. | 
|  | 48 | +// removed here is 2. | 
|  | 49 | +int removed = collection.RemoveRange(new[] { 1, 3, 5 }); | 
|  | 50 | +``` | 
|  | 51 | +> { 0, ~~1~~ 2, ~~3~~ 4, 8, 9 } | 
|  | 52 | +
 | 
|  | 53 | +</br> | 
|  | 54 | + | 
|  | 55 | +```csharp | 
|  | 56 | +collection.InsertRange(index: 2, collection: Enumerable.Range(10, 7)); | 
|  | 57 | +``` | 
|  | 58 | +> { 0, 2, 10, 11, 12, 13, 14, 15, 16, 4, 8, 9 } | 
|  | 59 | +
 | 
|  | 60 | +</br> | 
|  | 61 | + | 
|  | 62 | +```csharp | 
|  | 63 | +// This method is roughly equivalent to RemoveRange, then InsertRange. | 
|  | 64 | +// When index and count are equal to 0, it is equivalent to InsertRange(0, collection). | 
|  | 65 | +// changed here is 0. | 
|  | 66 | +int changed = collection.ReplaceRange(index: 6, count: 3, new[] { -1, -2, -3 }); | 
|  | 67 | +``` | 
|  | 68 | +> { 0, 2, 10, 11, 12, 13, -1, -2, -3, 4, 8, 9 } | 
|  | 69 | +
 | 
|  | 70 | +</br> | 
|  | 71 | + | 
|  | 72 | +```csharp | 
|  | 73 | +// Clears the current collection and replaces it with the specified item. | 
|  | 74 | +collection.Replace(42); | 
|  | 75 | +``` | 
|  | 76 | +> { 42 } | 
|  | 77 | +
 | 
|  | 78 | +</br> | 
|  | 79 | + | 
|  | 80 | +- If duplicate items are not allowed in the collection, set `AllowDuplicates = false`, and you can specify the `Comparer = xxx`. | 
|  | 81 | +- Most of the extended methods have return values to indicate changes in the number of collections. | 
|  | 82 | + | 
| 18 | 83 | ## Why WpfObservableRangeCollection? | 
| 19 | 84 | See [ObservableCollection Doesn't support AddRange method, so I get notified for each item added, besides what about INotifyCollectionChanging? - StackOverflow](https://stackoverflow.com/q/670577/4380178) | 
| 20 | 85 | 
 | 
| 21 |  | -I've searched the web for some ObservableCollections that have *Range methods, but they all raise various exceptions(and some strange problems) in certain specific situations: | 
|  | 86 | +I've searched the web for some ObservableCollections that have *\*Range* methods, but they all raise various exceptions(and some strange problems) in certain specific situations: | 
| 22 | 87 | - System.NotSupportedException: Range actions are not supported. | 
| 23 | 88 | - System.InvalidOperationException: The "x" index in the collection change event is not valid for collections of size "y". | 
| 24 | 89 | - More? I'm not sure. I forgot. | 
|  | 
0 commit comments