dotnet/wpf · error · ArgumentException

SR.GridCollection_InOtherCollection

Error message

SR.GridCollection_InOtherCollection

What it means

The Grid.ColumnDefinitions property setter throws ArgumentException (SR.GridCollection_InOtherCollection) when the assigned ColumnDefinitionCollection is already owned by another Grid (value.Owner != null && value.Owner != this). A definition collection can only belong to one grid at a time, so sharing it across grids is rejected.

Solutions

  1. Create a new ColumnDefinitionCollection with fresh ColumnDefinition objects for each Grid instead of sharing the collection.
  2. Clone the definitions: iterate the source collection and add equivalent ColumnDefinitions to the target grid's collection.
  3. If reuse was intended, clear/own the collection on the original grid first so its Owner is released, though a fresh collection is safer.

Example fix

// before
otherGrid.ColumnDefinitions = grid.ColumnDefinitions; // ArgumentException
// after
foreach (ColumnDefinition col in grid.ColumnDefinitions)
{
    otherGrid.ColumnDefinitions.Add(new ColumnDefinition
    {
        Width = col.Width,
        MinWidth = col.MinWidth,
        MaxWidth = col.MaxWidth
    });
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (source.ColumnDefinitions.Owner != null && source.ColumnDefinitions.Owner != targetGrid) { /* clone instead of assign */ }

Type guard

bool IsAssignableTo(Grid g, ColumnDefinitionCollection c) => c.Owner == null || c.Owner == g;

Try / catch

try { grid.ColumnDefinitions = candidate; }
catch (ArgumentException) { /* collection owned by another grid; clone it instead */ }

Prevention

When it happens

Trigger: Assigning grid2.ColumnDefinitions = grid1.ColumnDefinitions (or a collection obtained from another Grid's property), or re-assigning a collection whose Owner references a different Grid instance.

Common situations: Attempting to share a common column layout between two Grids by copying the collection reference; moving a definition collection from one grid to another without detaching; copy/paste refactorings that alias collection properties.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Grid.cs:283

        public ColumnDefinitionCollection ColumnDefinitions
        {
            get
            {
                if (_data == null) { _data = new ExtendedData(); }
                if (_data.ColumnDefinitions == null) { _data.ColumnDefinitions = new ColumnDefinitionCollection(this); }

                return (_data.ColumnDefinitions);
            }
            set
            {
                if (value?.Owner is not null)
                {
                    if (value.Owner == this)
                    {
                        return;
                    }

                    throw new ArgumentException(SR.Format(SR.GridCollection_InOtherCollection, nameof(value), nameof(ColumnDefinitionCollection)));
                }

                _data ??= new ExtendedData();
                if (_data.ColumnDefinitions is { } columnDefinitions)
                {
                    columnDefinitions.Owner = null;
                }

                _data.ColumnDefinitions = null;
                
                if (value is not null)
                {
                    value.Owner = this;
                    _data.ColumnDefinitions = value;
                }
            }
        }

View on GitHub (pinned to 81131a70a4)