dotnet/wpf · error · ArgumentException

SR.Format(SR.GridCollection_InOtherCollection…

Error message

SR.Format(SR.GridCollection_InOtherCollection, nameof(Grid), nameof(ColumnDefinitionCollection))

What it means

When the ColumnDefinitionCollection is attached to a Grid (OnPropertyChanged owner propagation), the setter throws ArgumentException with SR.GridCollection_InOtherCollection stating the definition already belongs to another Grid's ColumnDefinitionCollection. A DefinitionBase instance may live in exactly one Grid's collection at a time; shared instances would corrupt layout bookkeeping (_parent/owner links).

Solutions

  1. Give each Grid its own ColumnDefinition instances; do not reuse one across grids.
  2. Create definitions in code (new ColumnDefinition()) per grid instead of sharing resource instances.
  3. Before reusing, remove the definition from its current collection so its owner is cleared.
  4. If sharing is intentional, share the sizing parameters (GridLength values) and clone the definitions instead of the objects.

Example fix

<!-- before: shared resource used by two grids throws -->
<Window.Resources><ColumnDefinition x:Key="Col" Width="*"/></Window.Resources>
<Grid><Grid.ColumnDefinitions><StaticResource ResourceKey="Col"/></Grid.ColumnDefinitions></Grid>
<!-- after: define per grid -->
<Grid><Grid.ColumnDefinitions><ColumnDefinition Width="*"/></Grid.ColumnDefinitions></Grid>
Defensive patterns

Strategy: validation

Validate before calling

if (colDef.Parent is Grid other && other != targetGrid)
    other.ColumnDefinitions.Remove(colDef); // detach before reuse

Type guard

static bool IsFreeForGrid(ColumnDefinition d, Grid target) => !(d.Parent is Grid g) || ReferenceEquals(g, target);

Try / catch

try { targetGrid.ColumnDefinitions.Add(colDef); }
catch (ArgumentException ex) when (ex.Message.Contains("other collection"))
{ targetGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = colDef.Width }); }

Prevention

When it happens

Trigger: Assigning a shared ColumnDefinition (often a XAML resource or a static/shared field) as an item into collections of more than one Grid, or reparenting a collection without clearing it from the previous Grid first.

Common situations: Defining a ColumnDefinition as a reused <Window.Resources> resource applied to several DataGrids/Grids, cloning templates that share definition instances, or moving a definitions collection between grids in code.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

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

        /// <summary>
        ///    Owner property.
        /// </summary>
        internal Grid Owner
        {
            get => _owner;
            set
            {
                if (_owner == value)
                {
                    return;
                }

                if (_owner is null)
                {
                    if (value.ColumnDefinitions.Count > 0)
                    {
                        throw new ArgumentException(SR.Format(SR.GridCollection_InOtherCollection, nameof(Grid), nameof(ColumnDefinitionCollection)));
                    }
                    _owner = value;
                    PrivateOnModified();
                    for (int i = 0; i < _size; i++)
                    {
                        DefinitionBase item = _items[i];
                        _owner.AddLogicalChild(item);
                        item.OnEnterParentTree();
                    }
                }
                else if (value is null)
                {
                    PrivateOnModified();
                    for (int i = 0; i < _size; i++)
                    {
                        DefinitionBase item = _items[i];
                        item.OnExitParentTree();
                        _owner.RemoveLogicalChild(item);

View on GitHub (pinned to 81131a70a4)