Do something before collection changes in watched assembly in wpf

I'm not sure what I'm trying to achieve, is actually achievable or not.

I have an observed team with me, and its replacement event has already been processed. What I want to do is want to make some changes to the existing list of objects in the observable collection just before the observable collectible collection event is truncated. In other words, I want to do something with the existing list of objects in the observable assembly before anyone adds or removes any object from the observed collection. Something like handling a collection event, but, unfortunately, there is no such event in the observed collector. I hope I was clear enough.

+3
source share
4 answers

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

Instead, consider creating your own collection class that derives from an ObservableCollection , and then overriding the Add (), Insert (), and Remove () methods to do additional processing before invoking the base class implementation, you should be able to find examples of this on the Internet.

Here is a sample code to get you started. This comes from the 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");
    }
}
+3
source

, : , .
, , ObservableCollection<T> , Add CollectionChanging, Add . .

, , . , ?

+2

, ObservableCollection , ( ):

  • ObservableCollection.
  • ObservableCollection.
  • ObservableCollection.

"", , CollectionChanged, , "YOU" (: - ) , .

, , , , ...

0

INotifyCollectionChanged, , , .

, , , , ,

0

All Articles