dotnet/wpf · error · ArgumentException
SR.TextSchema_TheChildElementBelongsToAnotherTreeAlready
Error message
SR.TextSchema_TheChildElementBelongsToAnotherTreeAlready
What it means
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.
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.
Example fix
// before secondParagraph.Inlines.Add(existingRun); // run already in firstParagraph // after var clone = new Run(existingRun.Text); secondParagraph.Inlines.Add(clone);
Defensive patterns
Strategy: validation
Validate before calling
if (child is TextElement te && te.Parent != null) throw new InvalidOperationException("element already belongs to a tree"); Type guard
bool IsUnownedTextElement(object child) => !(child is TextElement te) || te.Parent == null;
Try / catch
try { ... } catch (ArgumentException ex) { /* clone the element and retry */ } Prevention
- Clone TextElements when duplicating content between trees
- Remove an element from its parent before re-parenting
- Treat TextElements as single-owner objects
When it happens
Trigger: 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.
Common situations: Cloning or duplicating document content by re-referencing existing elements instead of copying; moving elements between documents.
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
- Current DocumentSequence, FixedDocument, or FixedPage not…
- IAmbientProvider
- InvalidOperationException()
- IXamlSchemaContextProvider
- Microsoft.Windows.Controls.SR.ElementNotKeyTipScope
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/185879ba600a01fb.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/ValidationHelper.cs:115
{
Invariant.Assert(position != null);
if (child == null)
{
throw new ArgumentNullException(paramName);
}
if (!TextSchema.IsValidChild(/*position:*/position, /*childType:*/child.GetType()))
{
throw new ArgumentException(SR.Format(SR.TextSchema_ChildTypeIsInvalid, position.Parent.GetType().Name, child.GetType().Name));
}
// The new child should not be currently in other text tree
if (child is TextElement)
{
if (((TextElement)child).Parent != null)
{
throw new ArgumentException(SR.Format(SR.TextSchema_TheChildElementBelongsToAnotherTreeAlready, child.GetType().Name));
}
}
else
{
Invariant.Assert(child is UIElement);
// Cannot call UIElement.Parent across assembly boundary. So skip this part of validation. This condition will be checked elsewhere anyway.
//if (((UIElement)child).Parent != null)
//{
// throw new ArgumentException(SR.Format(SR.TextSchema_TheChildElementBelongsToAnotherTreeAlready, child.GetType().Name));
//}
}
}
#endregion Internal methods
}
}
View on GitHub (pinned to 81131a70a4)