dotnet/wpf · error · ArgumentException

SR.Format(SR.TableCollectionElementTypeExpected…

Error message

SR.Format(SR.TableCollectionElementTypeExpected, nameof(TableColumn))

What it means

TableColumnCollection's explicit IList.Add only accepts TableColumn instances; passing any other object throws ArgumentException (TableCollectionElementTypeExpected) naming TableColumn as the expected element type. This guards the strongly-typed collection against loosely-typed IList access.

Solutions

  1. Cast/convert the value to TableColumn before adding
  2. Use the strongly-typed TableColumnCollection.Add(TableColumn) API instead of IList.Add
  3. Check 'value is TableColumn' before the call to fail gracefully

Example fix

// before
((IList)table.Columns).Add(new TableRow()); // wrong element type
// after
table.Columns.Add(new TableColumn());
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is TableColumn col) table.Columns.Add(col); else throw new ArgumentException(nameof(value));

Type guard

static bool IsTableColumn(object v) => v is TableColumn;

Try / catch

try { list.Add(value); } catch (ArgumentException ex) when (ex.ParamName == "value") { /* wrong element type */ }

Prevention

When it happens

Trigger: Calling (table.Columns as IList).Add(value) — or IList-typed Add — where value is null or not a TableColumn (e.g. a TableRow, string, or UIElement).

Common situations: Reflection or late-binding code adding columns via the non-generic IList interface; XAML/data-binding that supplies wrong child elements into Table.Columns.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TableColumnCollection.cs:278

        }

        #endregion Public Methods

        //-------------------------------------------------------------------
        //
        //  IList Members
        //
        //-------------------------------------------------------------------

        #region IList Members

        int IList.Add(object value)
        {
            TableColumn item = value as TableColumn;

            if (item == null)
            {
                throw new ArgumentException(SR.Format(SR.TableCollectionElementTypeExpected, nameof(TableColumn)), nameof(value));
            }

            return ((IList)_columnCollection).Add(value);
        }

        void IList.Clear()
        {
            this.Clear();
        }

        bool IList.Contains(object value)
        {
            return ((IList)_columnCollection).Contains(value);
        }

        int IList.IndexOf(object value)
        {
            return ((IList)_columnCollection).IndexOf(value);

View on GitHub (pinned to 81131a70a4)