{"record":{"id":"7e397509174247c8","repo":"dotnet/wpf","slug":"sr-format-sr-textschema","errorCode":null,"errorMessage":"SR.Format(SR.TextSchema_TheChildElementBelongsToAnotherTreeAlready, value.GetType().Name)","messagePattern":"SR\\.Format\\(SR\\.TextSchema_TheChildElementBelongsToAnotherTreeAlready, value\\.GetType\\(\\)\\.Name\\)","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FlowDocument.cs","lineNumber":1632,"sourceCode":"        ///<summary>\n        /// Called to Add the object as a Child.\n        ///</summary>\n        ///<param name=\"value\">\n        /// Object to add as a child\n        ///</param>\n        void IAddChild.AddChild(Object value)\n        {\n            ArgumentNullException.ThrowIfNull(value);\n\n            if (!TextSchema.IsValidChildOfContainer(/*parentType:*/_typeofThis, /*childType:*/value.GetType()))\n            {\n                throw new ArgumentException(SR.Format(SR.TextSchema_ChildTypeIsInvalid, _typeofThis.Name, value.GetType().Name));\n            }\n\n            // Checking that the element inserted does not have a parent\n            if (value is TextElement && ((TextElement)value).Parent != null)\n            {\n                throw new ArgumentException(SR.Format(SR.TextSchema_TheChildElementBelongsToAnotherTreeAlready, value.GetType().Name));\n            }\n\n            if (value is Block)\n            {\n                TextContainer textContainer = _structuralCache.TextContainer;\n                ((Block)value).RepositionWithContent(textContainer.End);\n            }\n            else\n            {\n                Invariant.Assert(false); // We do not expect anything except Blocks on top level of a FlowDocument\n            }\n        }\n\n        ///<summary>\n        /// Called when text appears under the tag in markup\n        ///</summary>\n        ///<param name=\"text\">\n        /// Text to Add to the Object","sourceCodeStart":1614,"sourceCodeEnd":1650,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FlowDocument.cs#L1614-L1650","documentation":"After validating the child type, FlowDocument.IAddChild.AddChild checks that a TextElement child is not already parented to another tree. Adding an element that already has a Parent would reparent it and corrupt the previous document, so the method throws ArgumentException with SR.TextSchema_TheChildElementBelongsToAnotherTreeAlready, including the child's type name.","triggerScenarios":"Calling IAddChild.AddChild (or Blocks.Add / Inlines.Add) with a TextElement (Paragraph, Run, Section...) whose Parent is non-null — i.e. the same element instance is being inserted into a second document or a second location.","commonSituations":"Reusing a Paragraph/Run instance across two FlowDocuments; cloning UI templates that share element instances; moving a Block between documents without detaching it first (e.g. in copy/paste or template code).","solutions":["Detach the child first: ((Block)value).Reposition(null, null) or remove it from its current Blocks/Inlines collection before adding","Create a new element instance (or a deep clone) instead of reusing one across trees","Check value is TextElement te && te.Parent is null before adding","Catch ArgumentException and clone the subtree programmatically (e.g. via XamlWriter/XamlReader round-trip)"],"exampleFix":"// before\ndoc2.Blocks.Add(sharedParagraph); // throws if sharedParagraph.Parent != null\n// after\nif (sharedParagraph.Parent is BlockCollection bc) bc.Remove(sharedParagraph);\ndoc2.Blocks.Add(sharedParagraph);","handlingStrategy":"validation","validationCode":"if (value is System.Windows.Documents.TextElement te && te.Parent != null)\n    throw new ArgumentException($\"{te.GetType().Name} already belongs to another tree; detach or clone first.\");","typeGuard":"bool CanAddToTree(System.Windows.Documents.TextElement el) => el.Parent is null;","tryCatchPattern":"try { doc.Blocks.Add(block); }\ncatch (ArgumentException) {\n    block.Reposition(null, null); // detach\n    doc.Blocks.Add(block);\n}","preventionTips":["Never reuse a TextElement instance across two documents","Remove the element from its current parent collection before re-adding","Clone subtrees (XamlWriter/XamlReader round-trip) when copying between documents","Check Parent is null before every insertion"],"tags":["wpf","flowdocument","xaml","tree"],"backgroundTag":"type-mismatch","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"}