{"record":{"id":"e08851eea778bb85","repo":"dotnet/wpf","slug":"sr-tablecollectioninothercollection-e08851","errorCode":null,"errorMessage":"SR.TableCollectionInOtherCollection","messagePattern":"SR\\.TableCollectionInOtherCollection","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TableTextElementCollectionInternal.cs","lineNumber":45,"sourceCode":"        /// </summary>\n        /// <param name=\"item\">The TItem to be added to the end of the ContentElementCollection.</param>\n        /// <returns>The ContentElementCollection index at which the TItem has been added.</returns>\n        /// <remarks>Adding a null is prohibited.</remarks>\n        /// <exception cref=\"ArgumentNullException\">\n        /// If the <c>item</c> value is null.\n        /// </exception>\n        /// <exception cref=\"ArgumentException\">\n        /// If the new child already has a parent.\n        /// </exception>\n        public override void Add(TElementType item)\n        {\n            Version++;\n\n            ArgumentNullException.ThrowIfNull(item);\n\n            if (item.Parent != null)\n            {\n                throw new System.ArgumentException(SR.TableCollectionInOtherCollection);\n            }\n\n            Owner.InsertionIndex = Size;\n            item.RepositionWithContent(Owner.ContentEnd);\n            Owner.InsertionIndex = -1;\n        }\n\n        /// <summary>\n        /// Removes all elements from the ContentElementCollection.\n        /// </summary>\n        /// <remarks>\n        /// Count is set to zero. Capacity remains unchanged.\n        /// To reset the capacity of the ContentElementCollection, call TrimToSize\n        /// or set the Capacity property directly.\n        /// </remarks>\n        public override void Clear()\n        {\n            Version++;","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TableTextElementCollectionInternal.cs#L27-L63","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Remove the item from its current parent collection before adding it to the new one.","Create a fresh instance (or deep copy) instead of reusing an already-parented element.","Guard with if (item.Parent == null) before calling Add, and log/skip otherwise.","Use the destination's Move/insert APIs designed for reparenting rather than Add, where available."],"exampleFix":"// before\notherTable.RowGroups.Add(rowGroup); // rowGroup already parented -> throws\n// after\nif (rowGroup.Parent is TableRowGroupCollection old)\n{\n    old.Remove(rowGroup);\n}\notherTable.RowGroups.Add(rowGroup);","handlingStrategy":"validation","validationCode":"if (item.Parent != null)\n{\n    // detach or copy first\n}\nelse\n{\n    collection.Add(item);\n}","typeGuard":"static bool IsAttachable(TextElement e) => e != null && e.Parent == null;","tryCatchPattern":"try { collection.Add(item); }\ncatch (ArgumentException) { /* item already parented; remove from old parent or clone */ }","preventionTips":["Always remove an element from its old parent before adding it elsewhere","Clone elements when duplicating content between documents","Never share one element instance between two trees"],"tags":["wpf","argument-exception","table","document-tree"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}