Was considering the System.Collections.ObjectModel ObservableCollection<T>
class. This one is strange because
My need here is to add a batch of objects to a collection and the listener also gets the batch as part of the notification. Am I missing something with ObservableCollection ? Is there another class that meets my spec?
Update: Don't want to roll my own as far as feasible. I'd have to build in add/remove/change etc.. a whole lot of stuff.
Related Q:
https://stackoverflow.com/questions/670577/observablecollection-doesnt-support-addrange-method-so-i-get-notified-for-each
ListDictionary is faster than Hashtable for small collections (10 items or fewer). The Dictionary<TKey,TValue> generic class provides faster lookup than the SortedDictionary<TKey,TValue> generic class.
An ObservableCollection is a dynamic collection of objects of a given type. Objects can be added, removed or be updated with an automatic notification of actions. When an object is added to or removed from an observable collection, the UI is automatically updated.
Note that a class is a type. You can think of it as template used to create objects (also called instances of this class). So, you can have many different objects of the same class.
It seems that the INotifyCollectionChanged
interface allows for updating when multiple items were added, so I'm not sure why ObservableCollection<T>
doesn't have an AddRange
. You could make an extension method for AddRange
, but that would cause an event for every item that is added. If that isn't acceptable you should be able to inherit from ObservableCollection<T>
as follows:
public class MyObservableCollection<T> : ObservableCollection<T> { // matching constructors ... bool isInAddRange = false; protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { // intercept this when it gets called inside the AddRange method. if (!isInAddRange) base.OnCollectionChanged(e); } public void AddRange(IEnumerable<T> items) { isInAddRange = true; foreach (T item in items) Add(item); isInAddRange = false; var e = new NotifyCollectionChangedEventArgs( NotifyCollectionChangedAction.Add, items.ToList()); base.OnCollectionChanged(e); } }
Well the idea is same as that of fryguybob - kinda weird that ObservableCollection is kinda half-done. The event args for this thing do not even use Generics.. making me use an IList (that's so.. yesterday :) Tested Snippet follows...
using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; namespace MyNamespace { public class ObservableCollectionWithBatchUpdates<T> : ObservableCollection<T> { public void AddRange(ICollection<T> obNewItems) { IList<T> obAddedItems = new List<T>(); foreach (T obItem in obNewItems) { Items.Add(obItem); obAddedItems.Add(obItem); } NotifyCollectionChangedEventArgs obEvtArgs = new NotifyCollectionChangedEventArgs( NotifyCollectionChangedAction.Add, obAddedItems as System.Collections.IList); base.OnCollectionChanged(obEvtArgs); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With