Reputation: 1542
ObservableCollection
provides the CollectionChanged
event whenevet we add, remove or update the item from the colleciton, but it raises the event for individual item change, for example, whenever I add the new item to the collection it raises the collection.
Now, what I want is to raise the event after completing all the modifications to the collection, for example, I need to add 2 items, delete one item and update 2 items. The CollectionChanged
event should only fire after completiong all these add, delete and update.
Or, suppose I have a new collection with all the modifications, now , i want to raise the CollectionChanged
when I assign the new collection, for example:
ObservableCollection<string> mainCollection; //assume it has some items
mainCollection = updatedCollection; // at this point I want to raise the event.
Please provide your valuable suggestions.
Regards,
BHavik
Upvotes: 1
Views: 4730
Reputation: 6293
It won't work the way you written, the cause is that you've subscribed to event of mainCollection object and then replace it with whole another object just referencng it by the same variable name. You need not to assign collection but to add all elements of updated collection to main collection
EDIT: AddRange for ObservableCollection:
using System.Collections.Specialized;
using System.Collections.Generic;
namespace System.Collections.ObjectModel
{
/// <summary>
/// Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.
/// </summary>
/// <typeparam name="T"></typeparam>
public class ObservableCollection<T> : System.Collections.ObjectModel.ObservableCollection<T>
{
/// <summary>
/// Adds the elements of the specified collection to the end of the ObservableCollection(Of T).
/// </summary>
public void AddRange(IEnumerable<T> collection)
{
foreach (var i in collection) Items.Add(i);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add), collection.ToList());
}
/// <summary>
/// Removes the first occurence of each item in the specified collection from ObservableCollection(Of T).
/// </summary>
public void RemoveRange(IEnumerable<T> collection)
{
foreach (var i in collection) Items.Remove(i);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove), collection.ToList());
}
/// <summary>
/// Clears the current collection and replaces it with the specified item.
/// </summary>
public void Replace(T item)
{
ReplaceRange(new T[] { item });
}
/// <summary>
/// Clears the current collection and replaces it with the specified collection.
/// </summary>
public void ReplaceRange(IEnumerable<T> collection)
{
List<T> old = new List<T>(Items);
Items.Clear();
foreach (var i in collection) Items.Add(i);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace));
}
/// <summary>
/// Initializes a new instance of the System.Collections.ObjectModel.ObservableCollection(Of T) class.
/// </summary>
public ObservableCollection()
: base() { }
/// <summary>
/// Initializes a new instance of the System.Collections.ObjectModel.ObservableCollection(Of T) class that contains elements copied from the specified collection.
/// </summary>
/// <param name="collection">collection: The collection from which the elements are copied.</param>
/// <exception cref="System.ArgumentNullException">The collection parameter cannot be null.</exception>
public ObservableCollection(IEnumerable<T> collection)
: base(collection) { }
}
}
Taken from this question
Upvotes: 2
Reputation: 16980
Standard ObservableCollection<T>
doesn't support this scenario. You can create a class implementing INotifyCollectionChanged
interface with the logic you need, for example, with a single method to substitute all items with another collection.
That might not be good for UI, for example, if a collection-bound element was selected or focused this state will be lost if you completely re-initialize the collection. Depends on your scenario.
Upvotes: 1
Reputation: 9806
Looks like you'd rather be watching assignment to the mainCollection variable rather than the events that fire off the ObservableCollection class. Something like this:
private ObservableCollection<MyItemType> _mainCollection;
public ObservableCollection<MyItemType> MainCollection
{
get
{
return _mainCollection;
}
set
{
_mainCollection = value;
TriggerMyEvent(); // do whatever you like here
}
}
Upvotes: 2