Description
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