Home >Backend Development >C++ >How to Efficiently Notify an ObservableCollection of Item-Level Changes?

How to Efficiently Notify an ObservableCollection of Item-Level Changes?

Linda Hamilton
Linda HamiltonOriginal
2025-01-05 04:04:39267browse

How to Efficiently Notify an ObservableCollection of Item-Level Changes?

Notify ObservableCollection on Item Changes

In cases where items in an ObservableCollection undergo changes, it becomes necessary to notify the collection of these alterations to ensure proper UI updates. The default behavior of ObservableCollection falls short in observing item-level changes, prompting the exploration of alternative solutions.

TrulyObservableCollection

One approach involves utilizing the TrulyObservableCollection class, which extends ObservableCollection and includes a subscription mechanism for PropertyChanged events on its items. This can be implemented as follows:

public class TrulyObservableCollection<T> : ObservableCollection<T>
where T : INotifyPropertyChanged
{
    public TrulyObservableCollection()
        : base()
    {
        CollectionChanged += TrulyObservableCollection_CollectionChanged;
    }

    void TrulyObservableCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.NewItems != null)
        {
            foreach (Object item in e.NewItems)
            {
                (item as INotifyPropertyChanged).PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
            }
        }
        if (e.OldItems != null)
        {
            foreach (Object item in e.OldItems)
            {
                (item as INotifyPropertyChanged).PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);
            }
        }
    }

    void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        NotifyCollectionChangedEventArgs a = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
        OnCollectionChanged(a);
    }
}

Implementation Issues

However, using TrulyObservableCollection may not yield the desired results. To fully leverage its capabilities, the MyViewModel class must register a handler for the CollectionChanged event of MyItemsSource:

public MyViewModel()
{
    MyItemsSource.CollectionChanged += MyItemsSource_CollectionChanged;
}

Alternative Approach: Regular ObservableCollection

An alternative method is to employ a standard ObservableCollection and manually subscribe to item-level PropertyChanged events within the CollectionChanged handler:

public MyViewModel()
{
    MyItemsSource.CollectionChanged += MyItemsSource_CollectionChanged;
    
    // Add items to the collection
    MyItemsSource.Add(new MyType() { MyProperty = false });
    MyItemsSource.Add(new MyType() { MyProperty = true});
    MyItemsSource.Add(new MyType() { MyProperty = false });
}

void MyItemsSource_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    if (e.NewItems != null)
        foreach(MyType item in e.NewItems)
            item.PropertyChanged += MyType_PropertyChanged;

    if (e.OldItems != null)
        foreach(MyType item in e.OldItems)
            item.PropertyChanged -= MyType_PropertyChanged;
}

void MyType_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "MyProperty")
        DoWork();
}

With this approach, UI updates are triggered specifically when an item's MyProperty changes, eliminating the need for coarse updates across the entire collection.

The above is the detailed content of How to Efficiently Notify an ObservableCollection of Item-Level Changes?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn