Skip to content

[API Proposal]: NotifyCollectionChangedAction for item updated / item property changed #79713

Open
@paulober

Description

@paulober

Background and motivation

I currently have the problem of the ObservableCollection not issuing an CollectionChanged event when a property of an element changes. To solve this i implemented a custom "AdvancedObservableCollection" extending the ObservableCollection and only taking INotifyPropertyChanged classes. No every time a property of an element changes i trigger an OnCollectionChanged with NotifyCollectionChangedEventArgs.
The reason i've done all this, is to make a GridView control in WinUI 3 recognizing my changes to an element in the ItemsSource (the ObservableCollection).
Now all currently existent options like NotifyCollectionChangedAction.Reset or NotifyCollectionChangedAction.Replace causes to much updates in the client side and are not suitalbe for this type of usecase.

So i would like to suggest a new NotifyCollectionChangedAction like NotifyCollectionChangedAction.Update for the usecase of informing for Element/Item updates inside a collection that don't affect the structure of the collection only data inside one or more elements.

(Sorry for my english i'm not a native speaker.)

API Proposal

new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Updated, item));

API Usage

public class AdvancedObservableCollection<T>: ObservableCollection<T> where T : INotifyPropertyChanged
{
    public AdvancedObservableCollection()
    {
        CollectionChanged += CollectionChanged_Handler;
    }

    private void CollectionChanged_Handler(object? sender, NotifyCollectionChangedEventArgs e)
    {
        // unsubscribe all old objects
        if (e.OldItems != null)
        {
            foreach (T element in e.OldItems)
            {
                element.PropertyChanged -= Item_Changed;
            }
        }

        // subscribe all new objects
        if (e.NewItems != null)
        {
            foreach (T element in e.NewItems)
            {
                element.PropertyChanged += Item_Changed;
            }
        }
    }

    private void Item_Changed(object? sender, PropertyChangedEventArgs e)
    {
        if (sender is T item)
        {
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Updated, item));
        }
    }
}

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.CollectionsblockedIssue/PR is blocked on something - see commentswishlistIssue we would like to prioritize, but we can't commit we will get to it yet

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions