dotnet/wpf · error · InvalidOperationException

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

Error message

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

What it means

ItemCollection.RemoveAt throws InvalidOperationException(SR.MemberNotAllowedForView, "RemoveAt") when the underlying view does not implement IEditableCollectionView, which is required for index-based removal through the wrapper. The collection forwards the call only when the view supports editing.

Solutions

  1. Bind ItemsSource to an ObservableCollection<T> or another source whose view implements IEditableCollectionView.
  2. Guard with `items.IsEditingSupported`/CanRemove checks before calling RemoveAt.
  3. Remove from the source collection directly (it will propagate via collection-change notifications) when the view doesn't support RemoveAt.

Example fix

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

Strategy: validation

Validate before calling

if (items.CollectionView is IEditableCollectionView && index >= 0 && index < items.Count)
    items.RemoveAt(index);
else
    ((IList)itemsControl.ItemsSource).RemoveAt(index);

Type guard

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

Try / catch

try { items.RemoveAt(i); }
catch (InvalidOperationException) { ((IList)itemsControl.ItemsSource).RemoveAt(i); }

Prevention

When it happens

Trigger: Calling items.RemoveAt(index) when ItemsSource's view is not an IEditableCollectionView (e.g. ItemsSource bound to a source with a non-editable default view).

Common situations: Deleting rows in a list UI whose ItemsSource is a plain IEnumerable or a custom collection view; code tested against ObservableCollection later pointed at a different source.

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

Appendix: source

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

                }
            }
        }

        /// <summary>
        /// Remove the item at the given index from the underlying collection.
        /// The index is interpreted with respect to the view (not with respect to
        /// the underlying collection).
        /// </summary>
        void    IEditableCollectionView.RemoveAt(int index)
        {
            IEditableCollectionView ecv = _collectionView as IEditableCollectionView;
            if (ecv != null)
            {
                ecv.RemoveAt(index);
            }
            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"));
            }
        }

View on GitHub (pinned to 81131a70a4)