{"record":{"id":"9b66cf3389a58385","repo":"dotnet/wpf","slug":"sr-format-sr-textschema-childtypeisinvalid-typeofthis-name","errorCode":null,"errorMessage":"SR.Format(SR.TextSchema_ChildTypeIsInvalid, _typeofThis.Name, value.GetType().Name)","messagePattern":"SR\\.Format\\(SR\\.TextSchema_ChildTypeIsInvalid, _typeofThis\\.Name, value\\.GetType\\(\\)\\.Name\\)","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FlowDocument.cs","lineNumber":1626,"sourceCode":"        //  IAddChild Members\n        //\n        //-------------------------------------------------------------------\n\n        #region IAddChild Members\n\n        ///<summary>\n        /// Called to Add the object as a Child.\n        ///</summary>\n        ///<param name=\"value\">\n        /// Object to add as a child\n        ///</param>\n        void IAddChild.AddChild(Object value)\n        {\n            ArgumentNullException.ThrowIfNull(value);\n\n            if (!TextSchema.IsValidChildOfContainer(/*parentType:*/_typeofThis, /*childType:*/value.GetType()))\n            {\n                throw new ArgumentException(SR.Format(SR.TextSchema_ChildTypeIsInvalid, _typeofThis.Name, value.GetType().Name));\n            }\n\n            // Checking that the element inserted does not have a parent\n            if (value is TextElement && ((TextElement)value).Parent != null)\n            {\n                throw new ArgumentException(SR.Format(SR.TextSchema_TheChildElementBelongsToAnotherTreeAlready, value.GetType().Name));\n            }\n\n            if (value is Block)\n            {\n                TextContainer textContainer = _structuralCache.TextContainer;\n                ((Block)value).RepositionWithContent(textContainer.End);\n            }\n            else\n            {\n                Invariant.Assert(false); // We do not expect anything except Blocks on top level of a FlowDocument\n            }\n        }","sourceCodeStart":1608,"sourceCodeEnd":1644,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FlowDocument.cs#L1608-L1644","documentation":"FlowDocument implements IAddChild for XAML parsing, and only accepts children that TextSchema says are valid for a FlowDocument container (Block elements such as Paragraph, Section, Table, List). IAddChild.AddChild validates this first and throws ArgumentException with SR.TextSchema_ChildTypeIsInvalid naming the FlowDocument type and the offending child type.","triggerScenarios":"Calling IAddChild.AddChild on a FlowDocument with an object whose type is not a valid child (e.g. Run, Inline, UIElement, string), typically via XAML parsing or code that uses IAddChild directly.","commonSituations":"XAML mistakes like putting <Run> or raw text directly under <FlowDocument> (must be inside a Paragraph/Block); programmatic AddChild misuse in generated or parsed markup.","solutions":["Wrap inline content in a Block: <FlowDocument><Paragraph><Run>text</Run></Paragraph></FlowDocument>","Check TextSchema.IsValidChildOfContainer(typeof(FlowDocument), value.GetType()) before calling AddChild","Use the typed Blocks collection (flowDocument.Blocks.Add(block)) instead of IAddChild so invalid types are caught at compile time","Catch ArgumentException and report which child type was rejected"],"exampleFix":"// before\nflowDocument.Blocks.Add(new Run(\"hi\")); // or AddChild(new Run(\"hi\")) -> ArgumentException\n// after\nvar para = new Paragraph(new Run(\"hi\"));\nflowDocument.Blocks.Add(para);","handlingStrategy":"validation","validationCode":"if (!System.Windows.Documents.TextSchema.IsValidChildOfContainer(typeof(System.Windows.Documents.FlowDocument), value.GetType()))\n    throw new ArgumentException($\"{value.GetType().Name} is not a valid FlowDocument child; wrap in a Block (e.g. Paragraph).\");","typeGuard":"bool IsValidFlowDocumentChild(object o) =>\n    o is System.Windows.Documents.Block ||\n    System.Windows.Documents.TextSchema.IsValidChildOfContainer(typeof(System.Windows.Documents.FlowDocument), o.GetType());","tryCatchPattern":"try { ((System.Windows.Markup.IAddChild)doc).AddChild(value); }\ncatch (ArgumentException ex) { /* log invalid child type */ }","preventionTips":["In XAML always place inline content inside a Paragraph under FlowDocument","Use the typed Blocks collection instead of IAddChild","Validate child types with TextSchema before adding"],"tags":["wpf","flowdocument","xaml","argument"],"backgroundTag":"invalid-argument-value","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"}