dotnet/wpf · error

Exception of type 'System.InvalidOperationException' was…

Error message

Exception of type 'System.InvalidOperationException' was thrown.

What it means

MultipleCopiesCollection represents one repeated item, so the indexer's setter is meaningless and is implemented as `throw new InvalidOperationException()` in the indexor at MultipleCopiesCollection.cs:290. Replacing the value at any index is not a supported operation on this internal collection.

Solutions

  1. Do not write into the presenter's ItemsSource; edit the underlying data object or the collection bound to DataGrid.ItemsSource instead.
  2. Replace the whole item in your bound ObservableCollection if the row content must change.
  3. Cast checks: guard generic IList-editing helpers to skip read-only/fixed-size collections (IList.IsReadOnly / IsFixedSize).

Example fix

// before
cellsPresenter.ItemsSource[i] = newItem;

// after
boundRows[i] = newItem; // mutate the ObservableCollection bound to DataGrid.ItemsSource
Defensive patterns

Strategy: validation

Validate before calling

if (itemsSource is IList { IsReadOnly: true } or IList { IsFixedSize: true })
    throw new InvalidOperationException("Indexer writes are not allowed on this read-only collection.");

Type guard

bool SupportsIndexerWrite(object c) => c is IList l && !l.IsReadOnly && !l.IsFixedSize;

Try / catch

try { list[i] = value; }
catch (InvalidOperationException) { /* edit the underlying data item instead */ }

Prevention

When it happens

Trigger: Assigning through the indexer, e.g. `collection[i] = newItem`, typically via an IList-typed reference on the DataGrid cells presenter's ItemsSource.

Common situations: Generic editing code that swaps elements in any IList; attempts to patch a cell's content by writing into the presenter ItemsSource rather than the underlying data item.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/MultipleCopiesCollection.cs:290

        void IList.RemoveAt(int index)
        {
            throw new NotSupportedException(SR.DataGrid_ReadonlyCellsItemsSource);
        }

        public object this[int index]
        {
            get
            {
                ArgumentOutOfRangeException.ThrowIfNegative(index);
                ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, RepeatCount);

                Debug.Assert(_item != null, "_item should be non-null.");
                return _item;
            }

            set
            {
                throw new InvalidOperationException();
            }
        }

        #endregion

        #region ICollection Members

        public void CopyTo(Array array, int index)
        {
            throw new NotSupportedException();
        }

        public int Count
        {
            get { return RepeatCount; }
        }

        public bool IsSynchronized

View on GitHub (pinned to 81131a70a4)