{"record":{"id":"7052c118f6129c42","repo":"dotnet/wpf","slug":"sr-textboxinvalidchild","errorCode":null,"errorMessage":"SR.TextBoxInvalidChild","messagePattern":"SR\\.TextBoxInvalidChild","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TextBox.cs","lineNumber":130,"sourceCode":"        //\n        // -----------------------------------------------------------\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        ///<remarks>\n        /// This method will always throw InvalidOperationException because\n        /// the TextBox only accepts plain text.\n        ///</remarks>\n        void IAddChild.AddChild(Object value)\n        {\n            ArgumentNullException.ThrowIfNull(value);\n\n            // TextBox only accepts plain text, via IAddChild.AddText.\n            throw new InvalidOperationException(SR.Format(SR.TextBoxInvalidChild, value.ToString()));\n        }\n\n        ///<summary>\n        /// Called when text appears under the tag in markup.\n        ///</summary>\n        ///<param name=\"text\">\n        /// Text to Add to the Object\n        ///</param>\n        void IAddChild.AddText(string text)\n        {\n            ArgumentNullException.ThrowIfNull(text);\n\n            this.TextContainer.End.InsertTextInRun(text);\n        }\n\n        /// <summary>\n        /// Select the text in the given position and length.\n        /// </summary>","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TextBox.cs#L112-L148","documentation":"TextBox's explicit IAddChild.AddChild implementation always throws InvalidOperationException: a TextBox accepts only plain text through IAddChild.AddText, never child elements or objects. XAML that places any element inside a <TextBox> reaches this method and fails with the value's ToString in the message.","triggerScenarios":"Declaring XAML like <TextBox><TextBlock .../></TextBox> or <TextBox><SomeControl/></TextBox>; calling IAddChild.AddChild(obj) directly in code; any markup parser attempt to add an object child to TextBox.","commonSituations":"Copying layout from <TextBlock> (which does accept Inlines) into a <TextBox>; nesting a default/placeholder content element inside TextBox instead of using Text or Template parts; programmatic misuse of IAddChild.","solutions":["Set plain text via the Text property: <TextBox Text=\"hello\"/> instead of nesting a child.","For rich content, use TextBlock, RichTextBox (with FlowDocument), or a ContentControl — not TextBox.","If you need placeholder text, use the Tag-style watermark pattern (style with an adorner/VisualBrush) rather than a child element.","In code, replace addChild calls with textBox.Text = value.ToString() or textBox.AppendText(...)."],"exampleFix":"<!-- before: throws TextBoxInvalidChild -->\n<TextBox>\n    <TextBlock Text=\"Enter name\"/>\n</TextBox>\n\n<!-- after -->\n<TextBox Text=\"Enter name\"/>\n<!-- or for rich content -->\n<RichTextBox>\n    <FlowDocument>\n        <Paragraph>Enter name</Paragraph>\n    </FlowDocument>\n</RichTextBox>","handlingStrategy":"validation","validationCode":"// XAML review rule: a <TextBox> element must contain no child elements.\n// In code before calling IAddChild.AddChild:\nif (value is string s) { textBox.Text = s; }\nelse if (value is Control or TextBlock) { throw new NotSupportedException(\"Use TextBox.Text or RichTextBox instead of child elements.\"); }","typeGuard":"static bool IsPlainStringChild(object value) => value is string;","tryCatchPattern":"try\n{\n    ((IAddChild)textBox).AddChild(child);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"AddChild\") || ex.TargetSite.Name == \"AddChild\")\n{\n    textBox.Text = child?.ToString();\n}","preventionTips":["Never nest elements inside <TextBox> in XAML; use the Text attribute.","Use TextBlock or RichTextBox when rich/child content is required.","Implement placeholder text via styles/templates, not child elements.","In code, prefer Text/AppendText over the IAddChild interface."],"tags":["wpf","xaml","textbox","unsupported-child"],"backgroundTag":"unsupported-operation","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"}