dotnet/wpf · error · InvalidOperationException

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

Error message

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

What it means

ListCollectionView.AddNewItem(object) requires CanAddNewItem — the underlying collection must accept the supplied item (writable, correct type). When it cannot, the view throws InvalidOperationException with MemberNotAllowedForView("AddNewItem").

Solutions

  1. Check CanAddNewItem before calling AddNewItem and disable the add path otherwise
  2. Back the view with a writable collection (ObservableCollection<T> or List<T>)
  3. Ensure the item's type is compatible with the underlying collection's element type

Example fix

// before
view.AddNewItem(myItem); // throws on read-only source
// after
if (((IEditableCollectionView)view).CanAddNewItem)
    view.AddNewItem(myItem);
else
    throw new InvalidOperationException("Source collection is read-only");
Defensive patterns

Strategy: validation

Validate before calling

var ecv = (IEditableCollectionView)view;
if (ecv.CanAddNewItem)
{
    ecv.AddNewItem(myItem);
}
else { /* use a writable source collection */ }

Try / catch

try { view.AddNewItem(item); }
catch (InvalidOperationException)
{
    // cannot add the given item to the source collection
}

Prevention

When it happens

Trigger: Calling AddNewItem(item) when the underlying IList is read-only or fixed-size, or otherwise cannot contain the given item (CanAddNewItem false).

Common situations: Adding pre-constructed objects to a view over a read-only source; binding to arrays or immutable collections and then invoking AddNewItem from UI commands.

Related errors


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

Appendix: source

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

        /// <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="CommitNew"/> or <seealso cref="CancelNew"/> should be
        /// called to complete the transaction.
        /// </summary>
        public object AddNewItem(object newItem)
        {
            VerifyRefreshNotDeferred();

            if (IsEditingItem)
            {
                CommitEdit();   // implicitly close a previous EditItem
            }

            CommitNew();        // implicitly close a previous AddNew

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

            return AddNewCommon(newItem);
        }

        private object AddNewCommon(object newItem)
        {
            BindingOperations.AccessCollection(SourceList,
                () =>
                {
                    ProcessPendingChanges();    // bring the shadow list up to date

                    _newItemIndex = -2; // this is a signal that the next Add event comes from AddNew
                    int index = SourceList.Add(newItem);

                    // if the source doesn't raise collection change events, fake one
                    if (!(SourceList is INotifyCollectionChanged))
                    {
                        // the index returned by IList.Add isn't always reliable

View on GitHub (pinned to 81131a70a4)