dotnet/wpf · error · InvalidOperationException
SR.MemberNotAllowedDuringAddOrEdit (formatted with…
Error message
SR.MemberNotAllowedDuringAddOrEdit (formatted with "Sorting")
What it means
ListCollectionView throws InvalidOperationException when SortDescriptions are added or removed while the collection is in the middle of an AddNew or EditItem transaction. Sorting operations would require a refresh, which is not allowed while a pending new/edited item exists. Finish or cancel the transaction before changing sorting.
Solutions
- Commit or cancel the pending add/edit (CommitNew/CancelNew, CommitEdit/CancelEdit) before modifying SortDescriptions
- Check IsAddingNew / IsEditingItem and defer sorting changes until the transaction completes
- Subscribe to the view's transaction completion rather than applying sort changes immediately
Example fix
// before
view.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
// after
if (view.IsAddingNew) view.CommitNew();
if (view.IsEditingItem) view.CommitEdit();
view.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending)); Defensive patterns
Strategy: validation
Validate before calling
if (!view.IsAddingNew && !view.IsEditingItem) view.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending)); Try / catch
try { view.SortDescriptions.Add(desc); } catch (InvalidOperationException) { /* transaction open; defer sort change */ } Prevention
- Check IsAddingNew/IsEditingItem before any view mutation
- Commit or cancel add/edit transactions in UI event handlers promptly
- Centralize sorting changes in one helper that guards transaction state
When it happens
Trigger: Calling SortDescriptions.Add/Remove/Clear (or ModifyProperties like ListCollectionView.SortDescriptions.Add(new SortDescription(...))) between AddNew/AddNewItem and CommitNew/CancelNew, or between EditItem and CommitEdit/CancelEdit.
Common situations: Data-binding UI where a user sorts a DataGrid column while a row is in add/edit mode; code that reacts to collection-changed events and adjusts sorting while an edit transaction is open; re-sorting in response to a property change during an ongoing AddNew.
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
- SR.MemberNotAllowedDuringAddOrEdit (formatted with…
- ArgumentOutOfRangeException
- Current DocumentSequence, FixedDocument, or FixedPage not…
- IAmbientProvider
- index
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/02a10ec3281f0e92.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs:2906
if (_sort != null)
{
((INotifyCollectionChanged)_sort).CollectionChanged -= new NotifyCollectionChangedEventHandler(SortDescriptionsChanged);
}
_sort = descriptions;
if (_sort != null)
{
Invariant.Assert(_sort.Count == 0, "must be empty SortDescription collection");
((INotifyCollectionChanged)_sort).CollectionChanged += new NotifyCollectionChangedEventHandler(SortDescriptionsChanged);
}
}
// SortDescription was added/removed, refresh CollectionView
private void SortDescriptionsChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (IsAddingNew || IsEditingItem)
throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringAddOrEdit, "Sorting"));
// adding to SortDescriptions overrides custom sort
if (_sort.Count > 0)
{
_customSort = null;
}
RefreshOrDefer();
}
#region Grouping
// divide the data items into groups
private void PrepareGroups()
{
// if there's no grouping, there's nothing to do
if (!_isGrouping)
return;View on GitHub (pinned to 81131a70a4)