dotnet/wpf · error · InvalidOperationException

SR.Format(SR.MemberNotAllowedDuringTransaction…

Error message

SR.Format(SR.MemberNotAllowedDuringTransaction, "CancelNew", "EditItem")

What it means

CancelNew aborts an AddNew transaction, so it cannot run while an EditItem transaction is open. If IsEditingItem is true, ListCollectionView.CancelNew throws InvalidOperationException naming CancelNew and EditItem.

Solutions

  1. Call CommitEdit() or CancelEdit() to close the EditItem transaction before calling CancelNew
  2. Check IsEditingItem/IsAddingNew before cancelling, and route to CancelEdit vs CancelNew accordingly
  3. Ensure the UI commits or cancels the cell edit before cancelling the new-row transaction

Example fix

// before
collectionView.CancelNew(); // throws if EditItem is open
// after
var ecv = (IEditableCollectionView)collectionView;
if (ecv.IsEditingItem) ecv.CancelEdit();
if (ecv.IsAddingNew) ecv.CancelNew();
Defensive patterns

Strategy: validation

Validate before calling

var ecv = (IEditableCollectionView)view;
if (ecv.IsEditingItem) ecv.CancelEdit();
if (ecv.IsAddingNew) ecv.CancelNew();

Try / catch

try { view.CancelNew(); }
catch (InvalidOperationException)
{
    // an EditItem transaction is open; cancel/commit it first
}

Prevention

When it happens

Trigger: Calling CancelNew() while an EditItem transaction is active — e.g. user pressed Escape on a new row while a cell-edit transaction was still open, or code called EditItem after AddNew and then tried CancelNew.

Common situations: DataGrid cancel-row-edit handlers racing with cell-level edit transactions; MVVM cancel commands that don't check which transaction is currently 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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/b8bc518d6bb4a857. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs:941

            _group.RemoveSpecialItem(index, newItem, false /*loading*/);

            // now pretend it just got added to the collection.  This will add it
            // to the internal list with sort/filter, and to the groups
            ProcessCollectionChanged(
                    new NotifyCollectionChangedEventArgs(
                                NotifyCollectionChangedAction.Add,
                                newItem,
                                newItemIndex));
        }

        /// <summary>
        /// Complete the transaction started by <seealso cref="AddNew"/>.  The new
        /// item is removed from the collection.
        /// </summary>
        public void CancelNew()
        {
            if (IsEditingItem)
                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringTransaction, "CancelNew", "EditItem"));
            VerifyRefreshNotDeferred();

            if (_newItem == NoNewItem)
                return;

            // remove the new item from the underlying collection.  Normally the
            // collection will raise a Remove event, which we'll handle by calling
            // EndNew to leave AddNew mode.
            BindingOperations.AccessCollection(SourceList,
                () =>
                {
                    ProcessPendingChanges();
                    SourceList.RemoveAt(_newItemIndex);

                    // if the collection doesn't raise events, do the work explicitly on its behalf
                    if (_newItem != NoNewItem)
                    {
                        int index = AdjustBefore(NotifyCollectionChangedAction.Remove, _newItem, _newItemIndex);

View on GitHub (pinned to 81131a70a4)