dotnet/wpf · error · InvalidOperationException

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

Error message

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

What it means

ItemCollection.AddNew throws InvalidOperationException(SR.MemberNotAllowedForView, "AddNew") when the underlying collection view does not implement IEditableCollectionView, so the AddNew transactional-add feature is unavailable. The ItemCollection only forwards the call when the view supports editing.

Solutions

  1. Ensure ItemsSource is bound to a collection whose default view implements IEditableCollectionView (e.g. ObservableCollection<T>).
  2. Feature-test first: `if (items.CanAddNew) items.AddNew();`.
  3. Fall back to Items.Add for non-editable views when no transactional add is needed.

Example fix

// before
var newItem = items.AddNew();
// after
if (!items.CanAddNew)
    throw new InvalidOperationException("View does not support AddNew");
var newItem = items.AddNew();
Defensive patterns

Strategy: validation

Validate before calling

if (items.CanAddNew)
    var obj = items.AddNew();
else
    var obj = items.AddNewFallback(); // e.g. items.Add(new T())

Type guard

static bool CanAdd(ItemCollection c) => c.CollectionView is IEditableCollectionView && c.CanAddNew;

Try / catch

try { return items.AddNew(); }
catch (InvalidOperationException) { var o = new T(); items.Add(o); return o; }

Prevention

When it happens

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

Common situations: Using AddNew with a read-only or plain collection source; switching ItemsSource at runtime to a type whose view lacks editing support while edit-related code still runs.

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

Appendix: source

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

            }
        }

        /// <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 = _collectionView 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 = _collectionView as IEditableCollectionView;
            if (ecv != null)
            {
                ecv.CommitNew();
            }
            else
            {

View on GitHub (pinned to 81131a70a4)