{"record":{"id":"a9ca671b3e783435","repo":"dotnet/wpf","slug":"sr-textschema-childtypeisinvalid","errorCode":null,"errorMessage":"SR.TextSchema_ChildTypeIsInvalid","messagePattern":"SR\\.TextSchema_ChildTypeIsInvalid","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TextBlock.cs","lineNumber":145,"sourceCode":"                throw new ArgumentException(SR.Format(SR.TextPanelIllegalParaTypeForIAddChild, \"value\", value.GetType()));\n            }\n\n            // Get parent of the text container. Note that it can be not a \"this\" TextBlock - in case\n            // when a TextBlock is used as a storage for plain TextBox - as an owner of a text container.\n            Type parentType = _complexContent.TextContainer.Parent.GetType();\n\n            Type valueType = value.GetType();\n\n            // Do implicit conversion to allowed inline type - if possible\n            if (!TextSchema.IsValidChildOfContainer(parentType, /*childType*/valueType))\n            {\n                if (value is UIElement)\n                {\n                    value = new InlineUIContainer((UIElement)value);\n                }\n                else\n                {\n                    throw new ArgumentException(SR.Format(SR.TextSchema_ChildTypeIsInvalid, parentType.Name, valueType.Name));\n                }\n            }\n\n            // Insert inline element into the text container\n            Invariant.Assert(value is Inline, \"Schema validation helper must guarantee that invalid element is not passed here\");\n\n            TextContainer textContainer = (TextContainer)_complexContent.TextContainer;\n\n            textContainer.BeginChange();\n            try\n            {\n                TextPointer endPosition = textContainer.End;\n                textContainer.InsertElementInternal(endPosition, endPosition, (Inline)value);\n            }\n            finally\n            {\n                textContainer.EndChange();\n            }","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TextBlock.cs#L127-L163","documentation":"TextBlock's IAddChild.AddChild throws ArgumentException when the child's type is not valid for the parent element under the text schema. The parent (e.g. a Paragraph) only accepts Inline children; a UIElement is auto-wrapped in InlineUIContainer, but any other type (e.g. a string object, control, or wrong FlowDocument element) is rejected.","triggerScenarios":"Calling AddChild (or XAML nesting) with a child that is neither Inline nor UIElement inside a TextBlock/Paragraph, e.g. adding a Button directly is wrapped, but adding a string object or another TextBlock is rejected by the schema validator.","commonSituations":"XAML mistakes like nesting block elements (Paragraph) directly inside TextBlock content; passing non-flow objects programmatically; porting FlowDocument XAML into TextBlock content.","solutions":["Wrap non-inline content in a valid Inline type: Run for text, InlineUIContainer for UIElements.","Use the Text property for plain strings instead of AddChild.","Check the text schema: only Inline (Run, Span, Bold, Italic, Hyperlink, LineBreak, InlineUIContainer) is accepted as child."],"exampleFix":"// before\ntextBlock.AddChild(new Paragraph(new Run(\"hi\")));\n// after\ntextBlock.AddChild(new Run(\"hi\")); // or textBlock.Text = \"hi\";","handlingStrategy":"type-guard","validationCode":"bool isValidChild = value is Inline || value is UIElement;\nif (!isValidChild) return; // or wrap first","typeGuard":"static Inline ToInline(object v) =>\n    v is Inline i ? i : v is UIElement u ? new InlineUIContainer(u) : null;","tryCatchPattern":"try { textBlock.AddChild(child); }\ncatch (ArgumentException ex) { Log.Error(\"Invalid TextBlock child for parent schema\", ex); }","preventionTips":["Wrap text in Run and UIElements in InlineUIContainer","Use TextBlock.Text for strings","Remember Paragraph is for FlowDocument, not TextBlock"],"tags":["wpf","textblock","schema","argument-exception"],"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"}