[API Proposal]: NotifyCollectionChangedEventArgs Reset Action not contains reset data
- Dominant language
- C#
- Stars
- 7.7k
- Forks
- 1.3k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 61
Description
### Background and motivation
when I watch INotifyCollectionChanged.CollectionChanged,I want to get the collection of origin data when reset event is triggered.
```csharp
private void OnOriginCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch(e.Action)
{
case NotifyCollectionChangedAction.Reset:
{
//i want get origin data before reset
break;
}
default: break;
}
}
INotifyCollectionChanged collection;
collection.CollectionChanged+= OnOriginCollectionChanged;
```
### API Proposal
```csharp
public class NotifyCollectionChangedEventArgs : EventArgs
{
public NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action, IList changedItems)
{
if (action != 0 && action != NotifyCollectionChangedAction.Remove && action != NotifyCollectionChangedAction.Reset)
{
throw new ArgumentException(SR.GetString("MustBeResetAddOrRemoveActionForCtor"), "action");
}
if (action == NotifyCollectionChangedAction.Reset)
{
//if (changedItems != null)
//{
// throw new ArgumentException(SR.GetString("ResetActionRequiresNullItem"), "action");
//}
InitializeRemove(action, changedItems, -1);
}
else
{
if (changedItems == null)
{
throw new ArgumentNullException("changedItems");
}
InitializeAddOrRemove(action, changedItems, -1);
}
}
}
public class ObservableCollection : Collection
{
protected override void ClearItems()
{
IList originItems = Items.ToList();
CheckReentrancy();
base.ClearItems();
OnPropertyChanged("Count");
OnPropertyChanged("Item[]");
//OnCollectionReset();
OnCollectionReset(originData);
}
private void OnCollectionReset(IList data)
{
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset,data));
}
}
```
### API Usage
```csharp
private void OnOriginCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch(e.Action)
{
case NotifyCollectionChangedAction.Reset:
{
IList data = e.OldItems; //i can get data from e.OldItems
break;
}
default: break;
}
}
INotifyCollectionChanged collection;
collection.CollectionChanged+= OnOriginCollectionChanged;
```
### Alternative Designs
_No response_
### Risks
this Proposal will copy Collection of data, will leading to ClearItems Method slow.
Contributor guide
Assessment
This issue has not been assessed yet.