dotnet/wpf · error · ArgumentException

SR.TableCollectionInOtherCollection

Error message

SR.TableCollectionInOtherCollection

What it means

TableTextElementCollectionInternal<T>.Add rejects items that already belong to a parent. Because each text-element item can only be positioned in one document tree at a time, adding an item whose Parent != null throws ArgumentException with SR.TableCollectionInOtherCollection. The item must be detached (or new) before it can be added.

Solutions

  1. Remove the item from its current parent collection before adding it to the new one.
  2. Create a fresh instance (or deep copy) instead of reusing an already-parented element.
  3. Guard with if (item.Parent == null) before calling Add, and log/skip otherwise.
  4. Use the destination's Move/insert APIs designed for reparenting rather than Add, where available.

Example fix

// before
otherTable.RowGroups.Add(rowGroup); // rowGroup already parented -> throws
// after
if (rowGroup.Parent is TableRowGroupCollection old)
{
    old.Remove(rowGroup);
}
otherTable.RowGroups.Add(rowGroup);
Defensive patterns

Strategy: validation

Validate before calling

if (item.Parent != null)
{
    // detach or copy first
}
else
{
    collection.Add(item);
}

Type guard

static bool IsAttachable(TextElement e) => e != null && e.Parent == null;

Try / catch

try { collection.Add(item); }
catch (ArgumentException) { /* item already parented; remove from old parent or clone */ }

Prevention

When it happens

Trigger: Adding a TableRow/TableCell/TableRowGroup (or another text element) that is already a child of another table or row group; re-adding a previously removed-but-not-detached element; sharing one element instance between two tables.

Common situations: Moving content between two document structures without removing it from the first, cloning logic that reuses element instances, and code that stores elements in caches then re-inserts them.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TableTextElementCollectionInternal.cs:45

        /// </summary>
        /// <param name="item">The TItem to be added to the end of the ContentElementCollection.</param>
        /// <returns>The ContentElementCollection index at which the TItem has been added.</returns>
        /// <remarks>Adding a null is prohibited.</remarks>
        /// <exception cref="ArgumentNullException">
        /// If the <c>item</c> value is null.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// If the new child already has a parent.
        /// </exception>
        public override void Add(TElementType item)
        {
            Version++;

            ArgumentNullException.ThrowIfNull(item);

            if (item.Parent != null)
            {
                throw new System.ArgumentException(SR.TableCollectionInOtherCollection);
            }

            Owner.InsertionIndex = Size;
            item.RepositionWithContent(Owner.ContentEnd);
            Owner.InsertionIndex = -1;
        }

        /// <summary>
        /// Removes all elements from the ContentElementCollection.
        /// </summary>
        /// <remarks>
        /// Count is set to zero. Capacity remains unchanged.
        /// To reset the capacity of the ContentElementCollection, call TrimToSize
        /// or set the Capacity property directly.
        /// </remarks>
        public override void Clear()
        {
            Version++;

View on GitHub (pinned to 81131a70a4)