{"record":{"id":"5418699ef75fe3a1","repo":"dotnet/wpf","slug":"sr-textschema-childtypeisinvalid-parent-type-name-childtype","errorCode":null,"errorMessage":"SR.TextSchema_ChildTypeIsInvalid (parent type name, childType.Name)","messagePattern":"SR\\.TextSchema_ChildTypeIsInvalid \\(parent type name, childType\\.Name\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextSchema.cs","lineNumber":144,"sourceCode":"            // Disallow nested hyperlink elements.\n            if (TextSchema.HasHyperlinkAncestor(parent))\n            {\n                if (typeof(Hyperlink).IsAssignableFrom(childType) ||\n                    typeof(AnchoredBlock).IsAssignableFrom(childType))\n                {\n                    if (throwIfIllegalHyperlinkDescendent)\n                    {\n                        throw new InvalidOperationException(SR.Format(SR.TextSchema_IllegalHyperlinkChild, childType));\n                    }\n                    return false;\n                }\n            }\n\n            bool isValidChild = IsValidChild(parent.GetType(), childType);\n\n            if (!isValidChild && throwIfIllegalChild)\n            {\n                throw new InvalidOperationException(SR.Format(SR.TextSchema_ChildTypeIsInvalid, parent.GetType().Name, childType.Name));\n            }\n\n            return isValidChild;\n        }\n\n        internal static bool IsValidChild(TextPointer position, Type childType)\n        {\n            return ValidateChild(position, childType, false /* throwIfIllegalChild */, false /* throwIfIllegalHyperlinkDescendent */);\n        }\n\n        internal static bool ValidateChild(TextPointer position, Type childType, bool throwIfIllegalChild, bool throwIfIllegalHyperlinkDescendent)\n        {\n            DependencyObject parent = position.Parent;\n\n            if (parent == null)\n            {\n                TextElement leftElement = position.GetAdjacentElementFromOuterPosition(LogicalDirection.Backward);\n                TextElement rightElement = position.GetAdjacentElementFromOuterPosition(LogicalDirection.Forward);","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextSchema.cs#L126-L162","documentation":"Overload of ValidateChild taking (TextElement parent, Type childType, ...) that validates a prospective child TYPE against the parent element. If childType is not a valid child of parent.GetType() per the text schema and throwIfIllegalChild is true, an InvalidOperationException naming the parent type and the child type is thrown. Used by APIs that plan insertions before an element exists.","triggerScenarios":"Calling validation-driven insertion APIs with a childType illegal for the parent, e.g. checking/adding typeof(Paragraph) as child of a Span, typeof(Run) as child of FlowDocument, or typeof(Table) under a Hyperlink, during TextRange edit planning (CreateImplicitRun / split operations).","commonSituations":"Programmatic FlowDocument construction with wrong nesting; framework-internal text edits (splitting runs, converting elements) hitting positions where the schema forbids the child type.","solutions":["Call TextSchema.IsValidChild(parent, childType) before attempting the insertion","Choose a schema-legal childType for the parent (Inline types under Span/Paragraph; Block types under FlowDocument/Section/TableCell)","Insert an intermediate container that makes the pair legal (e.g. wrap a Run in a Paragraph when a Block parent needs content)","Catch InvalidOperationException, parse parent/child names from the message, and correct the document structure"],"exampleFix":"// before\nif (span.Inlines.CanAdd(typeof(Paragraph))) { } // never valid; ValidateChild throws\n// after\nif (TextSchema.IsValidChild(span, typeof(Inline)))\n{\n    span.Inlines.Add(new Run(\"text\"));\n}","handlingStrategy":"validation","validationCode":"if (!TextSchema.IsValidChild(parent, childType)) { /* choose another childType or restructure */ }","typeGuard":"static bool IsLegalChildType(TextElement parent, Type childType) => TextSchema.IsValidChild(parent.GetType(), childType);","tryCatchPattern":"try { DoPlannedInsertion(parent, childType); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"ChildTypeIsInvalid\")) { FallbackToDefaultChildType(parent); }","preventionTips":["Probe with IsValidChild(parent, childType) before planning insertions","Use throwIfIllegalChild:false when merely probing schema legality","Map parent types to legal child types in a helper table"],"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"}