开发者

How to get changes in ObservableCollection

public ObservableCollection<IndividualEntityCsidClidDetail> IncludedMembers { get; set; }

Let say I have a reference to IncludedMembers I want an event to occur when collection items are开发者_如何转开发 added/removed/edited.


handle the CollectionChanged event

//register the event so that every time when there is a change in collection CollectionChangedMethod method will be called

    yourCollection.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler 
(CollectionChangedMethod);

Create a method like this

private void CollectionChangedMethod(object sender, NotifyCollectionChangedEventArgs e)
{
       //different kind of changes that may have occurred in collection
       if(e.Action == NotifyCollectionChangedAction.Add)
        {
            //your code
        }
        if (e.Action == NotifyCollectionChangedAction.Replace)
        {
            //your code
        }
        if (e.Action == NotifyCollectionChangedAction.Remove)
        {
            //your code
        }
        if (e.Action == NotifyCollectionChangedAction.Move)
        {
            //your code
        }
}


Just register to the collection's CollectionChanged event. It will raise events when you add or remove items or otherwise, change the contents of the collection.

If you want to receive events when properties of the items in the collection change, you'd need to make sure that the items are IObservable first then Subscribe() to the individual objects.


That is what observable collections are for.

Simply bind to the collection and you are sorted!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜