dotnet/wpf · error · ArgumentException

SR.Format(SR.GridCollection_InOtherCollection, "value"…

Error message

SR.Format(SR.GridCollection_InOtherCollection, "value", "ColumnDefinitionCollection")

What it means

The ColumnDefinition already has a Parent, i.e. it belongs to another (or the same) ColumnDefinitionCollection. WPF enforces that each definition is owned by exactly one Grid and throws ArgumentException otherwise.

Solutions

  1. Remove the definition from its current collection before adding it elsewhere
  2. Create a fresh ColumnDefinition for each Grid
  3. Clear Parent ownership by removing from the owning Grid first

Example fix

// before
if (col.Parent != null) otherGrid.ColumnDefinitions.Add(col);
// after
if (col.Parent != null) ((Grid)col.Parent).ColumnDefinitions.Remove(col);
otherGrid.ColumnDefinitions.Add(col);
Defensive patterns

Strategy: validation

Validate before calling

if (colDef.Parent != null) ((Grid)colDef.Parent).ColumnDefinitions.Remove(colDef);

Type guard

bool CanAdd(ColumnDefinition c, Grid target) => c.Parent == null || c.Parent == target;

Try / catch

try { grid.ColumnDefinitions.Add(colDef); } catch (ArgumentException) { /* detach and retry or clone */ }

Prevention

When it happens

Trigger: grid.ColumnDefinitions.Add(colDef) or Insert where colDef.Parent != null — the definition is already a child of another Grid (or is being re-added to the same Grid without removal).

Common situations: Sharing column definitions across Grids; re-adding a definition that was never removed; moving definitions between Grids at runtime.

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

Appendix: source

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

        }

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

            ColumnDefinition item = value as ColumnDefinition;

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

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

        /// <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);

            ColumnDefinition item = value as ColumnDefinition;

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

View on GitHub (pinned to 81131a70a4)