dotnet/wpf · error · NotSupportedException

SR.VirtualizedCellInfoCollection_IsReadOnly

Error message

SR.VirtualizedCellInfoCollection_IsReadOnly

What it means

ValidateIsReadOnly throws NotSupportedException when an attempt is made to modify the collection while IsReadOnly is true. The DataGrid selected-cells collection is read-only in most contexts (only the grid itself mutates it), so any mutating call on the public collection is rejected.

Solutions

  1. Check IsReadOnly before mutating and fall back to DataGrid selection APIs.
  2. Use the supported mutators (SelectedCells.Add/Remove/Clear) which respect selection rules.
  3. Set selection through DataGrid row/cell IsSelected properties or DataGrid.SelectedItem instead.
  4. Wrap mutation in try-catch (NotSupportedException) for defensive generic code.

Example fix

// before
var list = (ICollection<DataGridCellInfo>)dataGrid.SelectedCells;
list.Clear();
// after
dataGrid.SelectedCells.Clear(); // or clear via grid APIs
foreach (var cell in dataGrid.SelectedCells.ToList()) { /* read-only iteration */ }
Defensive patterns

Strategy: validation

Validate before calling

if (collection.IsReadOnly) throw new NotSupportedException("SelectedCells is read-only");

Try / catch

try { collection.Add(cell); } catch (NotSupportedException) { /* mutate via DataGrid selection APIs instead */ }

Prevention

When it happens

Trigger: Calling Add/Clear/Remove (or any mutation routed through ValidateIsReadOnly) on a VirtualizedCellInfoCollection instance obtained where write access is not allowed, e.g. via IList members on the public SelectedCells wrapper.

Common situations: Trying to mutate SelectedCells through an IList cast in reflection or generic code; custom selection management code that assumes a writable collection; using the internal collection outside its sanctioned mutation paths.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/VirtualizedCellInfoCollection.cs:1627

            if ((rowIndex >= 0) && (columnIndex >= 0) &&
                (rowIndex < owner.Items.Count) && (columnIndex < owner.Columns.Count))
            {
                DataGridColumn column = owner.ColumnFromDisplayIndex(columnIndex);
                ItemsControl.ItemInfo rowInfo = owner.ItemInfoFromIndex(rowIndex);

                return CreateCellInfo(rowInfo, column, owner);
            }
            else
            {
                return DataGridCellInfo.Unset;
            }
        }

        private void ValidateIsReadOnly()
        {
            if (IsReadOnly)
            {
                throw new NotSupportedException(SR.VirtualizedCellInfoCollection_IsReadOnly);
            }
        }

        private void AddRegionToList(CellRegion region, List<DataGridCellInfo> list)
        {
            Debug.Assert(list != null, "list should not be null.");

            for (int rowIndex = region.Top; rowIndex <= region.Bottom; rowIndex++)
            {
                ItemsControl.ItemInfo rowInfo = _owner.ItemInfoFromIndex(rowIndex);
                for (int columnIndex = region.Left; columnIndex <= region.Right; columnIndex++)
                {
                    DataGridColumn column = _owner.ColumnFromDisplayIndex(columnIndex);
                    DataGridCellInfo cellInfo = CreateCellInfo(rowInfo, column, _owner);
                    list.Add(cellInfo);
                }
            }
        }

View on GitHub (pinned to 81131a70a4)