dotnet/wpf · error · ArgumentException

SR.GridCollection_MustBeCertainType

Error message

SR.GridCollection_MustBeCertainType

What it means

PrivateValidateValueForAddition throws ArgumentNullException if value is null, and ArgumentException(SR.GridCollection_MustBeCertainType, ...) if the object added to RowDefinitionCollection is not a RowDefinition. The collection is strongly typed and rejects any foreign element type.

Solutions

  1. Ensure only RowDefinition instances are added; use ColumnDefinitionCollection for ColumnDefinition objects.
  2. If iterating generic DefinitionBase objects, pattern-match before adding: if (d is RowDefinition rd) rows.Add(rd).
  3. Validate element type before calling Add through the non-generic IList interface.

Example fix

// before
((IList)grid.RowDefinitions).Add(new ColumnDefinition());
// after
if (def is RowDefinition rowDef)
    ((IList)grid.RowDefinitions).Add(rowDef);
else
    ((IList)grid.ColumnDefinitions).Add((ColumnDefinition)def);
Defensive patterns

Strategy: type-guard

Validate before calling

public static void AddDefinition(Grid g, DefinitionBase d) {
  if (d is RowDefinition rd) g.RowDefinitions.Add(rd);
  else if (d is ColumnDefinition cd) g.ColumnDefinitions.Add(cd);
}

Type guard

bool IsAddableRow(Grid g, object value) => value is RowDefinition rd && rd.Parent == null;

Try / catch

try { rows.Add(value); } catch (ArgumentNullException) { } catch (ArgumentException) { /* not a RowDefinition */ }

Prevention

When it happens

Trigger: grid.RowDefinitions.Add(columnDefinition) — accidentally adding a ColumnDefinition to the rows collection; adding via non-generic IList: ((IList)grid.RowDefinitions).Add(someObject) with a non-RowDefinition object.

Common situations: Copy-paste between ColumnDefinitions/RowDefinitions code; reflection or XAML-generation code that adds generic DefinitionBase objects; dynamic IList usage bypassing compile-time type checking.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RowDefinition.cs:588

        {
            if (this.IsReadOnly)
            {
                throw new InvalidOperationException(SR.Format(SR.GridCollection_CannotModifyReadOnly, "RowDefinitionCollection"));
            }
        }

        /// <summary>
        ///     Throws if value is not something the collection expects.
        /// </summary>
        private void PrivateValidateValueForAddition(object value)
        {
            ArgumentNullException.ThrowIfNull(value);

            RowDefinition item = value as RowDefinition;

            if (item == null)
            {
                throw new ArgumentException(SR.Format(SR.GridCollection_MustBeCertainType, "RowDefinitionCollection", "RowDefinition"));
            }

            if (item.Parent != null)
            {
                throw new ArgumentException(SR.Format(SR.GridCollection_InOtherCollection, "value", "RowDefinitionCollection"));
            }
        }

        /// <summary>
        ///     Throws if value is not something the collection expects.
        ///     Returns true if value belongs to the collection.
        /// </summary>
        private bool PrivateValidateValueForRemoval(object value)
        {
            ArgumentNullException.ThrowIfNull(value);

            RowDefinition item = value as RowDefinition;

View on GitHub (pinned to 81131a70a4)