dotnet / dotnet/wpf

[API Proposal]: NotifyCollectionChangedEventArgs Reset Action not contains reset data

Open
#8,145 1 comment 2 reactions 0 assignees View on GitHub
API suggestion Investigate
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.