dotnet/wpf · error · ArgumentException

SR.GridCollection_InOtherCollection

Error message

SR.GridCollection_InOtherCollection

What it means

When a RowDefinitionCollection's owner (Grid) is being attached (via the collection's owner setter), the incoming Grid must have an empty RowDefinitions collection; otherwise WPF throws ArgumentException(SR.GridCollection_InOtherCollection, "Grid", "RowDefinitionCollection"). This guards against one collection being re-parented into a Grid that already owns its own rows.

Solutions

  1. Create a fresh RowDefinitionCollection per Grid rather than sharing instances.
  2. Clear the target Grid's RowDefinitions before attaching the collection.
  3. Copy definitions item-by-item: foreach RowDefinition rd in source create a clone and call targetGrid.RowDefinitions.Add(clone).

Example fix

// before
sharedGrid.RowDefinitions = otherGrid.RowDefinitions; // collection already has rows
// after
foreach (RowDefinition rd in otherGrid.RowDefinitions)
    targetGrid.RowDefinitions.Add(CloneRowDefinition(rd));
Defensive patterns

Strategy: validation

Validate before calling

public static void AttachRows(Grid target, RowDefinitionCollection source) {
  if (target.RowDefinitions.Count > 0)
      target.RowDefinitions.Clear();
}

Type guard

bool CanAttach(Grid target, RowDefinitionCollection coll) => target.RowDefinitions.Count == 0 && coll.Cast<object>().All(o => o is RowDefinition);

Try / catch

try { AttachRows(grid, sharedCollection); } catch (ArgumentException) { /* target grid already owns rows */ }

Prevention

When it happens

Trigger: Assigning a RowDefinitionCollection (e.g. via internal owner hookup or re-parenting a shared collection object) to a Grid whose RowDefinitions.Count > 0 — sharing one RowDefinitionCollection instance between two Grids.

Common situations: Copying row definitions by reusing the same collection object across controls; deserialization/templating scenarios that re-attach a populated collection to a Grid that already has default rows.

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

Appendix: source

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

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

                if (_owner is null)
                {
                    if (value.RowDefinitions.Count > 0)
                    {
                        throw new ArgumentException(SR.Format(SR.GridCollection_InOtherCollection, nameof(Grid), nameof(RowDefinitionCollection)));
                    }
                    _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)