dotnet/wpf · error · InvalidOperationException

SR.Format(SR.MemberNotAllowedForView, "CancelNew")

Error message

SR.Format(SR.MemberNotAllowedForView, "CancelNew")

What it means

ItemCollection.CancelNew throws InvalidOperationException(SR.MemberNotAllowedForView, "CancelNew") when the underlying view does not implement IEditableCollectionView, so there is no AddNew transaction to roll back. The wrapper only supports this on editing-capable views.

Solutions

  1. Use a collection source whose view implements IEditableCollectionView.
  2. Guard with `items.CanCancelNew` before calling CancelNew.
  3. Track add transactions yourself (dispose/rollback logic) if the view cannot be changed.

Example fix

// before
items.CancelNew();
// after
if (items.CanCancelNew)
    items.CancelNew();
Defensive patterns

Strategy: validation

Validate before calling

if (items.CanCancelNew)
    items.CancelNew();

Type guard

static bool CanCancel(ItemCollection c) => c.CollectionView is IEditableCollectionView;

Try / catch

try { items.CancelNew(); }
catch (InvalidOperationException) { /* view not editable; discard new item manually */ }

Prevention

When it happens

Trigger: Calling items.CancelNew() when ItemsSource's view is not an IEditableCollectionView.

Common situations: Cancel button handlers in add-item dialogs bound to sources without editable views; switching ItemsSource while an add transaction is open.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemCollection.cs:962

            {
                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedForView, "CommitNew"));
            }
        }

        /// <summary>
        /// Complete the transaction started by <seealso cref="IEditableCollectionView.AddNew"/>.  The new
        /// item is removed from the collection.
        /// </summary>
        void    IEditableCollectionView.CancelNew()
        {
            IEditableCollectionView ecv = _collectionView as IEditableCollectionView;
            if (ecv != null)
            {
                ecv.CancelNew();
            }
            else
            {
                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedForView, "CancelNew"));
            }
        }

        /// <summary>
        /// Returns true if an </seealso cref="IEditableCollectionView.AddNew"> transaction is in progress.
        /// </summary>
        bool    IEditableCollectionView.IsAddingNew
        {
            get
            {
                IEditableCollectionView ecv = _collectionView as IEditableCollectionView;
                if (ecv != null)
                {
                    return ecv.IsAddingNew;
                }
                else
                {
                    return false;

View on GitHub (pinned to 81131a70a4)