{"record":{"id":"43872a1efccb2cef","repo":"dotnet/wpf","slug":"sr-textpanelillegalparatypeforiaddchild","errorCode":null,"errorMessage":"SR.TextPanelIllegalParaTypeForIAddChild","messagePattern":"SR\\.TextPanelIllegalParaTypeForIAddChild","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TextBlock.cs","lineNumber":127,"sourceCode":"        //-------------------------------------------------------------------\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            EnsureComplexContent();\n\n            if (!(_complexContent.TextContainer is TextContainer))\n            {\n                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));","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TextBlock.cs#L109-L145","documentation":"TextBlock's IAddChild.AddChild throws ArgumentException when the text container used for complex content is not a full TextContainer (e.g. the TextBlock only stores plain text for a TextBox). Such a container cannot accept child objects through the XAML IAddChild path.","triggerScenarios":"Adding a child object to a TextBlock whose complex content was created without a TextContainer — typically a TextBlock acting as the plain-text storage of a TextBox — via XAML parsing or programmatic AddChild calls.","commonSituations":"XAML that places child elements inside a TextBox's inner TextBlock; programmatic AddChild on a TextBlock reused as TextBox storage; template scenarios where the visual TextBlock is not the intended content host.","solutions":["Do not add children to the TextBlock inside a TextBox template; use TextBox APIs instead.","Use a standalone TextBlock (with its own TextContainer) when inline content is needed.","Set the Text property rather than adding child paragraphs when the TextBlock is plain-text backed."],"exampleFix":"// before\ntextBoxVisualTextBlock.AddChild(new Run(\"hello\"));\n// after\ntextBlock.Text = \"hello\"; // or use a standalone TextBlock for inline children","handlingStrategy":"validation","validationCode":"if (!textBlock.Inlines-null-check && isTextBoxBackedVisual) // don't AddChild on TextBox's inner TextBlock\n    return;","typeGuard":"static bool AcceptsChildren(TextBlock tb) => !(tb is { Parent: TextBox }); // only standalone TextBlocks host complex content","tryCatchPattern":"try { textBlock.AddChild(child); }\ncatch (ArgumentException) { /* this TextBlock is plain-text storage; use Text property */ }","preventionTips":["Never modify the TextBlock inside a TextBox template","Use TextBox.Text for plain text scenarios","Add inline children only to standalone TextBlocks"],"tags":["wpf","textblock","iaddchild","argument-exception"],"backgroundTag":"incompatible-source-type","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"}