{"record":{"id":"185879ba600a01fb","repo":"dotnet/wpf","slug":"sr-textschema-thechildelementbelongstoanothertreealready","errorCode":null,"errorMessage":"SR.TextSchema_TheChildElementBelongsToAnotherTreeAlready","messagePattern":"SR\\.TextSchema_TheChildElementBelongsToAnotherTreeAlready","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/ValidationHelper.cs","lineNumber":115,"sourceCode":"        {\n            Invariant.Assert(position != null);\n\n            if (child == null)\n            {\n                throw new ArgumentNullException(paramName);\n            }\n\n            if (!TextSchema.IsValidChild(/*position:*/position, /*childType:*/child.GetType()))\n            {\n                throw new ArgumentException(SR.Format(SR.TextSchema_ChildTypeIsInvalid, position.Parent.GetType().Name, child.GetType().Name));\n            }\n\n            // The new child should not be currently in other text tree\n            if (child is TextElement)\n            {\n                if (((TextElement)child).Parent != null)\n                {\n                    throw new ArgumentException(SR.Format(SR.TextSchema_TheChildElementBelongsToAnotherTreeAlready, child.GetType().Name));\n                }\n            }\n            else\n            {\n                Invariant.Assert(child is UIElement);\n                // Cannot call UIElement.Parent across assembly boundary. So skip this part of validation. This condition will be checked elsewhere anyway.\n                //if (((UIElement)child).Parent != null)\n                //{\n                //    throw new ArgumentException(SR.Format(SR.TextSchema_TheChildElementBelongsToAnotherTreeAlready, child.GetType().Name));\n                //}\n            }\n        }\n\n        #endregion Internal methods\n    }\n}\n","sourceCodeStart":97,"sourceCodeEnd":132,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/ValidationHelper.cs#L97-L132","documentation":"ValidationHelper.ValidateChild throws ArgumentException(SR.TextSchema_TheChildElementBelongsToAnotherTreeAlready) when the child is a TextElement that already has a Parent, i.e. it already lives in some text tree. A TextElement can only belong to one tree at a time; inserting an attached element would corrupt the tree.","triggerScenarios":"Inserting a TextElement instance that is already a child of another TextElement/tree — e.g. reusing the same Run in two paragraphs, or moving an element without detaching it first.","commonSituations":"Cloning or duplicating document content by re-referencing existing elements instead of copying; moving elements between documents.","solutions":["Clone the element (e.g. XamlWriter/XamlReader round-trip or manual copy) before inserting into a new tree.","Remove/detach the element from its current parent first, then insert.","Create a new element instance instead of reusing the old one."],"exampleFix":"// before\nsecondParagraph.Inlines.Add(existingRun); // run already in firstParagraph\n// after\nvar clone = new Run(existingRun.Text);\nsecondParagraph.Inlines.Add(clone);","handlingStrategy":"validation","validationCode":"if (child is TextElement te && te.Parent != null) throw new InvalidOperationException(\"element already belongs to a tree\");","typeGuard":"bool IsUnownedTextElement(object child) => !(child is TextElement te) || te.Parent == null;","tryCatchPattern":"try { ... } catch (ArgumentException ex) { /* clone the element and retry */ }","preventionTips":["Clone TextElements when duplicating content between trees","Remove an element from its parent before re-parenting","Treat TextElements as single-owner objects"],"tags":["wpf","text-element","invalid-state"],"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"}