Skip to content

Improved INotifyCollectionChanged Handling #52

Description

@Grauenwolf

This is a carry over from .NET Framework.

If INotifyCollectionChanged.CollectionChanged -> NotifyCollectionChangedEventArgs.NewItems has more than one item, most WPF controls will throw an exception.

The main reason for this original design is that ObservableCollection doesn't have an AddRange method and the controls assum that nothing else will support INotifyCollectionChanged.

Activity

  1. dotMorten commented on Dec 4, 2018

    @dotMorten
    Contributor

    Yes! This has been extremely annoying. Being able to trigger one large change for a set of items without causing UI relayout over and over again would be a huge perf improvement. The interface completely supports this, but for some reason WPF never has. Currently our only choice is to trigger a reset event, which causes a complete relayout of all items.

  2. benaadams commented on Dec 4, 2018

    @benaadams
    Member

    The main reason for this original design is that ObservableCollection doesn't have an AddRange method

    The following apis have been approved for Collection<T> and ObservableCollection<T> in .NET Core; though not implemented yet https://github.com/dotnet/corefx/issues/10752:

    public partial class Collection<T>
    {
        public void AddRange(IEnumerable<T> collection);
        public void InsertRange(int index, IEnumerable<T> collection);
        public void RemoveRange(int index, int count);
        public void ReplaceRange(int index, int count, IEnumerable<T> collection);
    
        // These are override by ObservableCollection<T> to raise the event:
        protected virtual void InsertItemsRange(int index, IEnumerable<T> collection);
        protected virtual void RemoveItemsRange(int index, int count);
        protected virtual void ReplaceItemsRange(int index, int count, IEnumerable<T> collection);
    }
  3. dotMorten commented on Dec 4, 2018

    @dotMorten
    Contributor

    The main reason for this original design is that ObservableCollection doesn't have an AddRange method

    This isn't about ObservableCollection, but about INotifyCollectionChanged interface. Any collection can implement that interface and have add-range, or be adding items in "chunks" (like incrementally loading datasources etc)

  4. airbreather commented on Dec 4, 2018

    @airbreather

    most WPF controls will throw an exception.

    I was under the impression that it's "just" System.Windows.Data.CollectionView which would need to change, and that (in theory) something else could implement System.ComponentModel.ICollectionView to get the desired benefits.

    Admittedly, however, I haven't tried it... am I mistaken?

    Edit: that said, System.ComponentModel.ICollectionView itself extends System.Collections.Specialized.INotifyCollectionChanged, which makes me think that System.Windows.Data.CollectionView is likely to be just the most visible of, perhaps, many places that don't support batched changes...

  5. added
    Enhancement Requested
    on Dec 4, 2018
  6. added this to the Future milestone on Dec 4, 2018
  7. YZahringer commented on Apr 18, 2021

    @YZahringer

    Could be a 6.0.0 candidate? This currently blocks the range support in .net 6 ObservableCollection

  8. DamirLisak commented on Aug 16, 2023

    @DamirLisak

    In this context, I would like to point out the following problem:
    dotnet/efcore#12675
    EF core uses ObservableHashSet that implements INotifyCollectionChanged. However, the ObservableHashSet is an unsorted collection and is not able to specify an index in the OldStartingIndex property for a Remove action. This leads to an exception at this point in the EnumerableCollectionView.cs:


    In this case, the entire CollectionView would have to be reloaded.

  9. symbiogenesis commented on Jan 26, 2024

    @symbiogenesis

    This is one of the top-requested features in all of the world of .NET, going back many years. Offering this is crucial.

  10. dotMorten commented on Jul 5, 2024

    @dotMorten
    Contributor

    Suggestion: We don't we log an issue for each control that have a problem with multi-item changes, and that way we can start chipping away at it and hopefully get to a point where we can add AddRange to ObservableCollection<T> ? This issue could then be the epic for the individual tasks.

  11. symbiogenesis commented on Jul 8, 2024

    @symbiogenesis

    @anjali-wpf @singhashish-wpf

  12. symbiogenesis commented on Jul 8, 2024

    @symbiogenesis
  13. added
    Performance
    on Jul 30, 2024
  14. added a commit that references this issue on Sep 22, 2026
  15. 17 remaining items

  16. added 14 commits that reference this issue on Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    🚧 work in progressEnhancement RequestedPerformance

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions