Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

do something before collection changes in observablecollection in wpf

I am not sure what i am trying to achieve is actually achievable or not.

I have an observablecollection with me and its collectionchanged event is already been handled. What i want to do is I want to make some changes in the existing list of objects in the observablecollection just before the collectionchanged event of the observablecollection gets fired. In other words i want to do something to the existing list of objects in the observablecollection before anyone adds or removes any object from the observablecollection. Something like handling the collectionchanging event but unfortunately there is not such event in observablecollection. I hope i have been clear enough.

like image 630
samar Avatar asked Dec 10 '25 20:12

samar


2 Answers

Since you need to take action before the user changes the collection, I believe your CollectionChangedEvent is happening too late (the collection has already changed).

Instead, consider creating your own collection class which derives from ObservableCollection and then override the Add(), Insert(), and Remove() methods to do your additional processing before calling the base class implementation. You should be able to find examples of that on the web.

Here is some sample code to get you started. It derives from Collection:

public class MyCollection<T> : Collection<T>, INotifyCollectionChanged, INotifyPropertyChanged
{
    public MyCollection(Collection<T> list)
        : base(list)
    {
    }

    public MyCollection()
        : base()
    {
    }

    #region INotifyCollectionChanged Members

    public event NotifyCollectionChangedEventHandler CollectionChanged;

    protected void NotifyChanged(NotifyCollectionChangedEventArgs args)
    {
        NotifyCollectionChangedEventHandler handler = CollectionChanged;
        if (handler != null)
        {
            handler(this, args);
        }
    }
    #endregion

    public new void Add(T item)
    {
        // Do some additional processing here!

        base.Add(item);
        this.NotifyChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, base.Count-1));
        this.OnPropertyChanged("Count");
    }
}
like image 126
Philipp Schmid Avatar answered Dec 12 '25 10:12

Philipp Schmid


You have been clear enough and the simple answer is: There is no such event and it is not possible.
The only solution I can think of is to derive from ObservableCollection<T> and implement that functionality yourself, i.e. in your implementation of Add you would first raise the CollectionChanging event and then call the Add method of the base class. You would do the same for all other relevant methods.

Having said all that, I am not really sure, this is the correct way to do it. Can you provide a reason why you would need this functionality?

like image 44
Daniel Hilgarth Avatar answered Dec 12 '25 09:12

Daniel Hilgarth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!