dotnet/wpf · error · InvalidOperationException
SR.MemberNotAllowedDuringAddOrEdit (formatted with…
Error message
SR.MemberNotAllowedDuringAddOrEdit (formatted with "Grouping")
What it means
ListCollectionView throws InvalidOperationException when the GroupDescriptions collection raises its change event (OnGroupByChanged) while an AddNew or EditItem transaction is active. Grouping changes trigger a refresh which cannot happen mid-transaction. Complete or cancel the transaction first.
Solutions
- Commit or cancel the add/edit transaction before changing GroupDescriptions
- Guard grouping changes with checks on IsAddingNew/IsEditingItem and defer them
- Restructure code so grouping is configured once before any item transactions begin
Example fix
// before
view.GroupDescriptions.Add(new PropertyGroupDescription("Category"));
// after
if (view.IsAddingNew) view.CancelNew();
if (view.IsEditingItem) view.CancelEdit();
view.GroupDescriptions.Add(new PropertyGroupDescription("Category")); Defensive patterns
Strategy: validation
Validate before calling
if (!view.IsAddingNew && !view.IsEditingItem) view.GroupDescriptions.Add(new PropertyGroupDescription("Category")); Try / catch
try { view.GroupDescriptions.Add(gd); } catch (InvalidOperationException) { /* defer until transaction closes */ } Prevention
- Guard GroupDescriptions changes with transaction-state checks
- Avoid regrouping from CollectionChanged handlers that can fire mid-edit
- Configure grouping before starting item transactions
When it happens
Trigger: Adding/removing a GroupDescription (view.GroupDescriptions.Add(...)) while IsAddingNew or IsEditingItem is true; PropertyChanged on an existing GroupDescription fires during an open add/edit transaction.
Common situations: User regroups a list (e.g. changes a DataGrid grouping) while a new item is being added; programmatic grouping applied in a CollectionChanged handler during an edit; async UI updates changing grouping while a row editor is open.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- ArgumentOutOfRangeException
- SR.MemberNotAllowedDuringAddOrEdit (formatted with…
- index
- InvalidOperationException
- (nameof(value)) ArgumentNullException
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/fefd9db5fdd642cd.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs:2999
// For the Group to report collection changed
private void OnGroupChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
AdjustCurrencyForAdd(e.NewStartingIndex);
}
else if (e.Action == NotifyCollectionChangedAction.Remove)
{
AdjustCurrencyForRemove(e.OldStartingIndex);
}
OnCollectionChanged(e);
}
// The GroupDescriptions collection changed
private void OnGroupByChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (IsAddingNew || IsEditingItem)
throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringAddOrEdit, "Grouping"));
// This is a huge change. Just refresh the view.
RefreshOrDefer();
}
// A group description for one of the subgroups changed
private void OnGroupDescriptionChanged(object sender, EventArgs e)
{
if (IsAddingNew || IsEditingItem)
throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringAddOrEdit, "Grouping"));
// This is a huge change. Just refresh the view.
RefreshOrDefer();
}
// An item was inserted into the collection. Update the groups.
private void AddItemToGroups(object item, LiveShapingItem lsi)
{View on GitHub (pinned to 81131a70a4)