{"record":{"id":"79daa7ad50027f7a","repo":"dotnet/wpf","slug":"sr-textschema-childtypeisinvalid-validationhelper","errorCode":null,"errorMessage":"SR.TextSchema_ChildTypeIsInvalid","messagePattern":"SR\\.TextSchema_ChildTypeIsInvalid","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/ValidationHelper.cs","lineNumber":107,"sourceCode":"        // ...............................................................\n        //\n        // TextSchema Validation\n        //\n        // ...............................................................\n\n        // Checks whether it is valid to insert the child object at passed position.\n        internal static void ValidateChild(TextPointer position, object child, string paramName)\n        {\n            Invariant.Assert(position != null);\n\n            if (child == null)\n            {\n                throw new ArgumentNullException(paramName);\n            }\n\n            if (!TextSchema.IsValidChild(/*position:*/position, /*childType:*/child.GetType()))\n            {\n                throw new ArgumentException(SR.Format(SR.TextSchema_ChildTypeIsInvalid, position.Parent.GetType().Name, child.GetType().Name));\n            }\n\n            // The new child should not be currently in other text tree\n            if (child is TextElement)\n            {\n                if (((TextElement)child).Parent != null)\n                {\n                    throw new ArgumentException(SR.Format(SR.TextSchema_TheChildElementBelongsToAnotherTreeAlready, child.GetType().Name));\n                }\n            }\n            else\n            {\n                Invariant.Assert(child is UIElement);\n                // Cannot call UIElement.Parent across assembly boundary. So skip this part of validation. This condition will be checked elsewhere anyway.\n                //if (((UIElement)child).Parent != null)\n                //{\n                //    throw new ArgumentException(SR.Format(SR.TextSchema_TheChildElementBelongsToAnotherTreeAlready, child.GetType().Name));\n                //}","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/ValidationHelper.cs#L89-L125","documentation":"ValidationHelper.ValidateChild throws ArgumentException(SR.TextSchema_ChildTypeIsInvalid) when TextSchema.IsValidChild says the child's type is not permitted at the given TextPointer position. The WPF text schema defines which element types may appear as children of each container type (e.g. only certain elements under Table/TableCell/Paragraph).","triggerScenarios":"Inserting a child whose runtime type is schema-invalid at the position — e.g. adding a Paragraph directly under a TableCell, or a TextElement under a container expecting UIElement content.","commonSituations":"Building flow documents programmatically and mixing up the allowed hierarchy; auto-generated code inserting elements at the wrong nesting level.","solutions":["Wrap or insert the child in a schema-legal intermediate element (e.g. put content in a Paragraph, paragraphs in a Section).","Insert at a position whose Parent permits the child type.","Query TextSchema.IsValidChild in advance and choose the child type accordingly."],"exampleFix":"// before\ntableCell.Child = new Paragraph(...); // invalid\n// after\nvar cellContent = new TableCell(new Paragraph(new Run(\"text\")));","handlingStrategy":"validation","validationCode":"if (!TextSchema.IsValidChild(position, child.GetType())) throw new ArgumentException($\"{child.GetType().Name} not allowed under {position.Parent.GetType().Name}\");","typeGuard":null,"tryCatchPattern":"try { ... } catch (ArgumentException ex) { /* wrap child in a legal container or skip */ }","preventionTips":["Consult TextSchema.IsValidChild before inserting any element","Follow the documented flow-document hierarchy (Run inside Paragraph, etc.)"],"tags":["wpf","text-schema","type-mismatch"],"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-22T01:17:13.364Z"}