dotnet/wpf · error · InvalidOperationException

throw new…

Error message

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

What it means

CollectionViewProxy.AddNewItem(newItem) is the IEditableCollectionViewAddNewItem implementation; it forwards to the proxied view's IEditableCollectionViewAddNewItem. If the wrapped view does not implement that interface (null `ani` at CollectionViewProxy.cs:744), the proxy throws InvalidOperationException: adding a typed new item is not allowed for this view.

Solutions

  1. Check whether ProxiedView as IEditableCollectionViewAddNewItem is non-null (or use CanAddNewItem) before calling
  2. Fall back to AddNew() plus assigning properties when IEditableCollectionViewAddNewItem is unavailable
  3. Implement IEditableCollectionViewAddNewItem on the proxied view if typed adds are required

Example fix

// before
proxy.AddNewItem(newItem);
// after
if (proxy.CanAddNewItem)
{
    proxy.AddNewItem(newItem);
}
else if (proxy.CanAddNew)
{
    proxy.AddNew();
}
Defensive patterns

Strategy: validation

Validate before calling

if (!(proxy.ProxiedView is IEditableCollectionViewAddNewItem)) return;
proxy.AddNewItem(newItem);

Type guard

bool SupportsAddNewItem(CollectionViewProxy p) => p?.ProxiedView is IEditableCollectionViewAddNewItem;

Try / catch

try { proxy.AddNewItem(newItem); }
catch (InvalidOperationException ex) when (ex.Message.Contains("AddNewItem")) { /* fall back to AddNew + property assignment */ }

Prevention

When it happens

Trigger: Calling AddNewItem on a CollectionViewProxy whose ProxiedView implements IEditableCollectionView but not IEditableCollectionViewAddNewItem, or neither.

Common situations: Programmatic typed adds (distinct from AddNew) on views lacking the AddNewItem contract; custom collection views that only implemented part of the editable-collection surface.

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

Appendix: source

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

            }
        }

        /// <summary>
        /// Add a new item to the underlying collection.  Returns the new item.
        /// After calling AddNewItem 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 IEditableCollectionViewAddNewItem.AddNewItem(object newItem)
        {
            IEditableCollectionViewAddNewItem ani = ProxiedView as IEditableCollectionViewAddNewItem;
            if (ani != null)
            {
                return ani.AddNewItem(newItem);
            }
            else
            {
                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedForView, "AddNewItem"));
            }
        }

        #endregion IEditableCollectionViewAddNewItem

        #region ICollectionViewLiveShaping

        ///<summary>
        /// Gets a value that indicates whether this view supports turning live sorting on or off.
        ///</summary>
        bool ICollectionViewLiveShaping.CanChangeLiveSorting
        {
            get
            {
                ICollectionViewLiveShaping cvls = ProxiedView as ICollectionViewLiveShaping;
                return (cvls != null) ? cvls.CanChangeLiveSorting : false;
            }
        }

View on GitHub (pinned to 81131a70a4)