dotnet/wpf · error · ArgumentOutOfRangeException

SR.TableCollectionOutOfRange

Error message

SR.TableCollectionOutOfRange

What it means

The explicit IList.Insert on ColumnDefinitionCollection validates that index is between 0 and _size (inclusive, to allow appending). An index outside that range throws ArgumentOutOfRangeException with SR.TableCollectionOutOfRange.

Solutions

  1. Clamp the index: Math.Max(0, Math.Min(index, grid.ColumnDefinitions.Count)).
  2. Use Add() when appending — it avoids index math entirely.
  3. Fix the position computation so it derives from ColumnDefinitions.Count, not a Grid.Children index.

Example fix

// before
((IList)grid.ColumnDefinitions).Insert(grid.Children.IndexOf(el), colDef);
// after
int idx = Math.Max(0, Math.Min(grid.Children.IndexOf(el), grid.ColumnDefinitions.Count));
((IList)grid.ColumnDefinitions).Insert(idx, colDef);
Defensive patterns

Strategy: validation

Validate before calling

if (index < 0 || index > grid.ColumnDefinitions.Count)
    throw new ArgumentOutOfRangeException(nameof(index));

Type guard

static bool ValidInsertIndex(int i, int count) => i >= 0 && i <= count;

Try / catch

try { ((IList)grid.ColumnDefinitions).Insert(index, value); }
catch (ArgumentOutOfRangeException) { grid.ColumnDefinitions.Add(def as ColumnDefinition); }

Prevention

When it happens

Trigger: Calling ((IList)grid.ColumnDefinitions).Insert(i, newColumnDefinition) where i < 0 or i > grid.ColumnDefinitions.Count.

Common situations: Computing the insert position from a UI element index without converting to the definitions collection; inserting into an assumed-populated collection (Count 0) at a stored index.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/6e2697286aabd164. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ColumnDefinition.cs:228

            }
            else
            {
                return value.Index;
            }
        }

        /// <summary>
        ///     <see cref="IList.Insert"/>
        /// </summary>
        /// <remarks>
        ///     <paramref name="value"/> must be of ColumnDefinition type.
        /// </remarks>
        void IList.Insert(int index, object value)
        {
            PrivateVerifyWriteAccess();
            if (index < 0 || index > _size)
            {
                throw new ArgumentOutOfRangeException(SR.TableCollectionOutOfRange);
            }
            PrivateValidateValueForAddition(value);
            PrivateInsert(index, value as ColumnDefinition);
        }

        /// <summary>
        ///     <see cref="IList<T>.Insert"/>
        /// </summary>
        public void Insert(int index, ColumnDefinition value)   //  void IList<T>.Insert(int index, T item)
        {
            PrivateVerifyWriteAccess();
            if (index < 0 || index > _size)
            {
                throw new ArgumentOutOfRangeException(SR.TableCollectionOutOfRange);
            }
            PrivateValidateValueForAddition(value);
            PrivateInsert(index, value);
        }

View on GitHub (pinned to 81131a70a4)