dotnet/wpf · error · InvalidOperationException

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

Error message

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

What it means

ItemCollection.Remove throws InvalidOperationException(SR.MemberNotAllowedForView, "Remove") when the underlying view does not implement IEditableCollectionView. The wrapper delegates removal to the view and requires editing support.

Solutions

  1. Ensure ItemsSource's view implements IEditableCollectionView (e.g. bind to ObservableCollection<T>).
  2. Remove the item from the underlying source collection directly instead.
  3. Feature-check before calling, e.g. verify `items.CollectionView is IEditableCollectionView`.

Example fix

// before
items.Remove(selectedItem);
// after
var oc = (ObservableCollection<MyItem>)itemsControl.ItemsSource;
oc.Remove(selectedItem);
Defensive patterns

Strategy: validation

Validate before calling

if (items.CollectionView is IEditableCollectionView)
    items.Remove(item);
else
    ((IList)itemsControl.ItemsSource).Remove(item);

Type guard

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

Try / catch

try { items.Remove(item); }
catch (InvalidOperationException) { ((IList)itemsControl.ItemsSource).Remove(item); }

Prevention

When it happens

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

Common situations: Delete-item commands against a read-only or minimal collection source; swapping ItemsSource implementations at runtime so previously-working Remove calls start failing.

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

Appendix: source

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

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

        /// <summary>
        /// Remove the given item from the underlying collection.
        /// </summary>
        void    IEditableCollectionView.Remove(object item)
        {
            IEditableCollectionView ecv = _collectionView as IEditableCollectionView;
            if (ecv != null)
            {
                ecv.Remove(item);
            }
            else
            {
                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedForView, "Remove"));
            }
        }

        #endregion Removing items

        #region Transactional editing of an item

        /// <summary>
        /// Begins an editing transaction on the given item.  The transaction is
        /// completed by calling either <seealso cref="IEditableCollectionView.CommitEdit"/> or
        /// <seealso cref="IEditableCollectionView.CancelEdit"/>.  Any changes made to the item during
        /// the transaction are considered "pending", provided that the view supports
        /// the notion of "pending changes" for the given item.
        /// </summary>
        void    IEditableCollectionView.EditItem(object item)
        {
            IEditableCollectionView ecv = _collectionView as IEditableCollectionView;
            if (ecv != null)

View on GitHub (pinned to 81131a70a4)