canton7 / Stylet

A very lightweight but powerful ViewModel-First MVVM framework for WPF for .NET Framework and .NET Core, inspired by Caliburn.Micro.
MIT License
988 stars 143 forks source link

BindableCollection.AddRange calls CollectionChanged with e.NewItems == null #154

Closed uyaem closed 3 years ago

uyaem commented 4 years ago

When calling BindableCollection.AddRange, the CollectionChanged event gets raised as expected, but the event handler's NotifyCollectionChangedEventArgs.NewItems is always null.

Expected behaviour: The added/removed items should be listed in e.NewItems/e.OldItems, like when calling BindableCollection.Add(...).

Affected version: Stylet 1.3.4

canton7 commented 4 years ago

The problem is right here: ListCollectionView doesn't allow Add events with ranges of added items. The options are therefore 1) raise multiple Add events, or 2) raise a Reset event. We do the latter: if the user wants multiple Add events, they can call BindableCollection.Add multiple times themselves.

uyaem commented 4 years ago

Ah, I was wondering why you didn't simply use the NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction, IList, IList) constructor. 😃 The implementation in ListCollectionView seems silly/dated...

My BindableCollection is initialized with multiple entries. my View contains both a DataGrid for editing, as well as a "graph" which should reflect any data changes on the fly, therefore I need to attach PropertyChanged listeners to all entries. Seems like my "workaround" of using a loop with the BindableCollection.Add in the initialization must therefore become a permanent thing.

Thanks for the clarification. 👍