{"record":{"id":"afaf7d667789ebbc","repo":"dotnet/wpf","slug":"sr-textschema-childtypeisinvalid-parent-type-name-child-type","errorCode":null,"errorMessage":"SR.TextSchema_ChildTypeIsInvalid (parent type name, child type name)","messagePattern":"SR\\.TextSchema_ChildTypeIsInvalid \\(parent type name, child type name\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextSchema.cs","lineNumber":113,"sourceCode":"        {\n            return ValidateChild(parent, child, false /* throwIfIllegalChild */, false /* throwIfIllegalHyperlinkDescendent */);\n        }\n#endif\n\n        internal static bool ValidateChild(TextElement parent, TextElement child, bool throwIfIllegalChild, bool throwIfIllegalHyperlinkDescendent)\n        {\n            // Disallow nested hyperlink elements.\n            if (TextSchema.HasHyperlinkAncestor(parent) &&\n                TextSchema.HasIllegalHyperlinkDescendant(child, throwIfIllegalHyperlinkDescendent))\n            {\n                return false;\n            }\n\n            bool isValidChild = IsValidChild(parent.GetType(), child.GetType());\n\n            if (!isValidChild && throwIfIllegalChild)\n            {\n                throw new InvalidOperationException(SR.Format(SR.TextSchema_ChildTypeIsInvalid, parent.GetType().Name, child.GetType().Name));\n            }\n\n            return isValidChild;\n        }\n\n        internal static bool IsValidChild(TextElement parent, Type childType)\n        {\n            return ValidateChild(parent, childType, false /* throwIfIllegalChild */, false /* throwIfIllegalHyperlinkDescendent */);\n        }\n\n        internal static bool ValidateChild(TextElement parent, Type childType, bool throwIfIllegalChild, bool throwIfIllegalHyperlinkDescendent)\n        {\n            // Disallow nested hyperlink elements.\n            if (TextSchema.HasHyperlinkAncestor(parent))\n            {\n                if (typeof(Hyperlink).IsAssignableFrom(childType) ||\n                    typeof(AnchoredBlock).IsAssignableFrom(childType))\n                {","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextSchema.cs#L95-L131","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nSpan span = new Span();\nspan.Inlines.Add(new Paragraph(new Run(\"x\"))); // invalid: Paragraph is a Block\n// after\nParagraph p = new Paragraph();\np.Inlines.Add(new Run(\"x\"));\n// or: BlockUIContainer / Section for block content","handlingStrategy":"validation","validationCode":"if (!TextSchema.IsValidChild(parent, child)) throw new ArgumentException($\"{child.GetType().Name} cannot be a child of {parent.GetType().Name}\");","typeGuard":"static bool IsLegalChild(TextElement parent, TextElement child) => TextSchema.IsValidChild(parent.GetType(), child.GetType());","tryCatchPattern":"try { parent.Inlines.Add(child); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"ChildTypeIsInvalid\")) { /* restructure tree */ }","preventionTips":["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"],"tags":["wpf","textschema","document-tree"],"backgroundTag":"schema-validation-failed","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"}