dotnet/wpf · error · InvalidOperationException

throw new…

Error message

throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedForView, "AddNew"));

What it means

CollectionViewProxy.AddNew throws InvalidOperationException(MemberNotAllowedForView, "AddNew") when the underlying view does not implement IEditableCollectionView, meaning the proxied collection cannot support the AddNew transaction.

Solutions

  1. Guard with view.CanAddNew (or an IEditableCollectionView check) before calling AddNew
  2. Bind UI to an editable collection (e.g. ObservableCollection or List-backed view)
  3. Disable add-new UI when the current view lacks editing support

Example fix

// before
var newItem = ((IEditableCollectionView)view).AddNew();
// after
var ecv = view as IEditableCollectionView;
if (ecv != null && ecv.CanAddNew)
    var newItem = ecv.AddNew();
Defensive patterns

Strategy: type-guard

Validate before calling

if (view is IEditableCollectionView ecv && ecv.CanAddNew) { var item = ecv.AddNew(); }

Type guard

bool CanAddNewItem(ICollectionView v) => v is IEditableCollectionView ecv && ecv.CanAddNew;

Try / catch

try { proxy.AddNew(); } catch (InvalidOperationException) { /* view not editable; disable add-new command */ }

Prevention

When it happens

Trigger: Calling IEditableCollectionView.AddNew via a CollectionViewProxy wrapping a non-editable view (view over a non-IList source, or a read-only collection).

Common situations: Toolbar 'Add new item' commands bound to views over read-only or non-editable sources; CanAddNew not checked before invoking AddNew; swapping data sources so the proxy no longer wraps an editable view.

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/255aee923363c1a3. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/CollectionViewProxy.cs:446

            }
        }

        /// <summary>
        /// Add a new item to the underlying collection.  Returns the new item.
        /// After calling AddNew and changing the new item as desired, either
        /// <seealso cref="IEditableCollectionView.CommitNew"/> or <seealso cref="IEditableCollectionView.CancelNew"/> should be
        /// called to complete the transaction.
        /// </summary>
        object IEditableCollectionView.AddNew()
        {
            IEditableCollectionView ecv = ProxiedView as IEditableCollectionView;
            if (ecv != null)
            {
                return ecv.AddNew();
            }
            else
            {
                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedForView, "AddNew"));
            }
        }


        /// <summary>
        /// Complete the transaction started by <seealso cref="IEditableCollectionView.AddNew"/>.  The new
        /// item remains in the collection, and the view's sort, filter, and grouping
        /// specifications (if any) are applied to the new item.
        /// </summary>
        void IEditableCollectionView.CommitNew()
        {
            IEditableCollectionView ecv = ProxiedView as IEditableCollectionView;
            if (ecv != null)
            {
                ecv.CommitNew();
            }
            else
            {

View on GitHub (pinned to 81131a70a4)