dotnet/wpf · error · InvalidOperationException
SR.TextSchema_ChildTypeIsInvalid (parent type name, child…
Error message
SR.TextSchema_ChildTypeIsInvalid (parent type name, child type name)
What it means
ValidateChild(TextElement parent, TextElement child, ...) checks whether the child element's type is a legal child of the parent's type per the WPF text schema (Table 2: Paragraph can contain Inline, etc.). If it is not and throwIfIllegalChild is true, an InvalidOperationException naming both type names is thrown. This guards the document tree from schema-invalid parent/child relationships.
Solutions
- Check TextSchema.IsValidChild(parent, child) before adding the element
- Restructure: wrap Block children in Paragraph (or use an Inline-compat wrapper) so the parent/child pair matches the schema, e.g. Span can only contain Inline
- Use the correct container type for the child (e.g. use Paragraph instead of Span when adding Block content)
- Catch InvalidOperationException and inspect the two type names in the message to correct the hierarchy
Example fix
// before
Span span = new Span();
span.Inlines.Add(new Paragraph(new Run("x"))); // invalid: Paragraph is a Block
// after
Paragraph p = new Paragraph();
p.Inlines.Add(new Run("x"));
// or: BlockUIContainer / Section for block content Defensive patterns
Strategy: validation
Validate before calling
if (!TextSchema.IsValidChild(parent, child)) throw new ArgumentException($"{child.GetType().Name} cannot be a child of {parent.GetType().Name}"); Type guard
static bool IsLegalChild(TextElement parent, TextElement child) => TextSchema.IsValidChild(parent.GetType(), child.GetType());
Try / catch
try { parent.Inlines.Add(child); } catch (InvalidOperationException ex) when (ex.Message.Contains("ChildTypeIsInvalid")) { /* restructure tree */ } Prevention
- Know the schema: Inline under Span/Paragraph/Hyperlink; Block under FlowDocument/Section/TableCell
- Call TextSchema.IsValidChild before every Add
- Sanitize pasted content structure before inserting into the tree
When it happens
Trigger: Adding a TextElement of an illegal type to a parent, e.g. appending a Paragraph to a Span, inserting a Table inside a Hyperlink, adding a Run directly to a FlowDocument, or TextRange/selection edits that would produce such a combination.
Common situations: Building FlowDocuments in code with wrong nesting (Block inside Inline container); pasting content whose structure doesn't fit; custom editors applying text element types to the wrong container.
Understand the failure class
Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.
Related errors
- SR.TextSchema_ChildTypeIsInvalid (parent type name…
- SR.TableCollectionInOtherCollection
- SR.TextSchema_IllegalHyperlinkChild (childType)
- " }} " element found. Expected fixed page element ( }} ).
- ' ' can only host a ' ' or a ' '. ' ' is an invalid…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/afaf7d667789ebbc.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextSchema.cs:113
{
return ValidateChild(parent, child, false /* throwIfIllegalChild */, false /* throwIfIllegalHyperlinkDescendent */);
}
#endif
internal static bool ValidateChild(TextElement parent, TextElement child, bool throwIfIllegalChild, bool throwIfIllegalHyperlinkDescendent)
{
// Disallow nested hyperlink elements.
if (TextSchema.HasHyperlinkAncestor(parent) &&
TextSchema.HasIllegalHyperlinkDescendant(child, throwIfIllegalHyperlinkDescendent))
{
return false;
}
bool isValidChild = IsValidChild(parent.GetType(), child.GetType());
if (!isValidChild && throwIfIllegalChild)
{
throw new InvalidOperationException(SR.Format(SR.TextSchema_ChildTypeIsInvalid, parent.GetType().Name, child.GetType().Name));
}
return isValidChild;
}
internal static bool IsValidChild(TextElement parent, Type childType)
{
return ValidateChild(parent, childType, false /* throwIfIllegalChild */, false /* throwIfIllegalHyperlinkDescendent */);
}
internal static bool ValidateChild(TextElement parent, Type childType, bool throwIfIllegalChild, bool throwIfIllegalHyperlinkDescendent)
{
// Disallow nested hyperlink elements.
if (TextSchema.HasHyperlinkAncestor(parent))
{
if (typeof(Hyperlink).IsAssignableFrom(childType) ||
typeof(AnchoredBlock).IsAssignableFrom(childType))
{View on GitHub (pinned to 81131a70a4)