Sanath Shetty
Sanath Shetty

Reputation: 484

how to get the notification on change in ObservableCollection object

is there a way to get notification or event when a new item is added or existing is updated in a ObservableCollection . say

class Notify:INotifyPropertyChanged
 {
     private string userID { get; set; }
     public string UserID
    {
        get { return userID; }

        set
        {
            if (userID != value)
            {
                userID = value;
                OnPropertyChanged("UserID");
            }
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
 }

 class MainClass
{

  ObservableCollection<Notify> notifyme = new ObservableCollection<Notify>();


 changed()
 {
    //logic where there is an update
 }

}

when do i call changed()

Upvotes: 1

Views: 5304

Answers (3)

Nick
Nick

Reputation: 798

You can use something like this

public class NotifyCollection
{
    private readonly ObservableCollection<Notify> collection;

    public NotifyCollection()
    {
        collection = new ObservableCollection<Notify>();
        collection.CollectionChanged += collection_CollectionChanged;
    }

    private void collection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if ((e.Action == NotifyCollectionChangedAction.Remove || e.Action == NotifyCollectionChangedAction.Replace) && e.OldItems != null)
        {
            foreach (var oldItem in e.OldItems)
            {
                var item = (Notify)oldItem;
                try
                {
                    item.PropertyChanged -= notify_changes;
                }
                catch { }
            }
        }

        if((e.Action==NotifyCollectionChangedAction.Add || e.Action==NotifyCollectionChangedAction.Replace) && e.NewItems!=null)
        {
            foreach(var newItem in e.NewItems)
            {
                var item = (Notify)newItem;
                item.PropertyChanged += notify_changes;
            }
        }

        notify_changes(null, null);
    }

    private void notify_changes(object sender, PropertyChangedEventArgs e)
    {
        //notify code here
    }

    public ObservableCollection<Notify> Collection
    {
        get
        {
            return collection;
        }
    }
}

Upvotes: 1

Abhinav Sharma
Abhinav Sharma

Reputation: 453

what i think is that INotifyPropertyChanged notifies the propertychanged event but here i think your collection is changed. So you have to raise a CollectionChanged Event.

I`ll suggest you to look into this and this.

Hope this help you!!!!

Upvotes: 1

slugster
slugster

Reputation: 49974

There is only really one way to do this: hook up an event handler on each item as (or before) you add it to the ObservableCollection.

notifyme.Add(new Notify{ PropertyChanged += (o, e) => { do whatever }});

This is because the ObservableCollection is simply a container, each item in it has to be individually hooked up. Of course you could write your own extending class (or extension method) that helps automate this.

Upvotes: 2

Related Questions