dotnet/wpf · error · ArgumentException
SR.Format(SR.GridCollection_InOtherCollection…
Error message
SR.Format(SR.GridCollection_InOtherCollection, nameof(ColumnDefinitionCollection), nameof(Grid))
What it means
A ColumnDefinition being added to (or already in) a ColumnDefinitionCollection still has a Parent, meaning it already belongs to another Grid's collection. A definition child can only ever be owned by one Grid, so WPF throws ArgumentException to enforce single ownership.
Solutions
- Give each Grid its own new ColumnDefinition instances instead of sharing
- Remove the definition from its current Grid's collection (otherGrid.ColumnDefinitions.Remove(colDef)) before adding it to the new one
- Clone the definition: new ColumnDefinition { Width = old.Width, MinWidth = old.MinWidth, ... }
Example fix
// before
foreach (var grid in grids) grid.ColumnDefinitions.Add(sharedColumn);
// after
foreach (var grid in grids) grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); Defensive patterns
Strategy: validation
Validate before calling
if (colDef.Parent != null) throw new InvalidOperationException("ColumnDefinition already belongs to another Grid; create a new instance or remove it first."); Type guard
bool IsUnowned(ColumnDefinition c) => c.Parent == null;
Try / catch
try { grid.ColumnDefinitions.Add(colDef); } catch (ArgumentException) { grid.ColumnDefinitions.Add(CloneColumnDefinition(colDef)); } Prevention
- Never share ColumnDefinition instances between Grids
- Clone definitions when a template-like set of columns is needed
- Check Parent before adding a definition programmatically
When it happens
Trigger: Calling grid.ColumnDefinitions.Add(colDef) or Insert(colDef, i) where colDef.Parent != null — i.e. the ColumnDefinition was previously added to another Grid (or the same Grid twice via a different path) and never removed.
Common situations: Sharing a single set of ColumnDefinitions between multiple Grid instances in XAML/code; re-adding a definition after moving a control around in code; building a Grid in a loop and reusing one definition instance as a template.
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
- SR.Format(SR.GridCollection_InOtherCollection, "value"…
- SR.Format(SR.GridCollection_MustBeCertainType…
- SR.GridCollection_DestArrayInvalidLength
- SR.GridCollection_DestArrayInvalidRank
- SR.GridCollection_InOtherCollection
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/13d28105a341a6c2.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ColumnDefinition.cs:540
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);
}
_owner = null;
}
else
{
throw new ArgumentException(SR.Format(SR.GridCollection_InOtherCollection, nameof(ColumnDefinitionCollection), nameof(Grid)));
}
}
}
/// <summary>
/// Internal version of Count.
/// </summary>
internal int InternalCount
{
get { return (_size); }
}
/// <summary>
/// Internal accessor to items array.
/// </summary>
internal DefinitionBase[] InternalItems
{
get { return (_items); }View on GitHub (pinned to 81131a70a4)