dotnet/wpf · error · InvalidOperationException
SR.TextBoxInvalidChild
Error message
SR.TextBoxInvalidChild
What it means
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.
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(...).
Example fix
<!-- before: throws TextBoxInvalidChild -->
<TextBox>
<TextBlock Text="Enter name"/>
</TextBox>
<!-- after -->
<TextBox Text="Enter name"/>
<!-- or for rich content -->
<RichTextBox>
<FlowDocument>
<Paragraph>Enter name</Paragraph>
</FlowDocument>
</RichTextBox> Defensive patterns
Strategy: validation
Validate before calling
// XAML review rule: a <TextBox> element must contain no child elements.
// In code before calling IAddChild.AddChild:
if (value is string s) { textBox.Text = s; }
else if (value is Control or TextBlock) { throw new NotSupportedException("Use TextBox.Text or RichTextBox instead of child elements."); } Type guard
static bool IsPlainStringChild(object value) => value is string;
Try / catch
try
{
((IAddChild)textBox).AddChild(child);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("AddChild") || ex.TargetSite.Name == "AddChild")
{
textBox.Text = child?.ToString();
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- Animation_ChildMustBeKeyFrame
- Animation_ChildMustBeKeyFrame
- Animation_NoTextChildren
- Animation_NoTextChildren
- ArgumentException: path
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/7052c118f6129c42.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TextBox.cs:130
//
// -----------------------------------------------------------
///<summary>
/// Called to Add the object as a Child.
///</summary>
///<param name="value">
/// Object to add as a child
///</param>
///<remarks>
/// This method will always throw InvalidOperationException because
/// the TextBox only accepts plain text.
///</remarks>
void IAddChild.AddChild(Object value)
{
ArgumentNullException.ThrowIfNull(value);
// TextBox only accepts plain text, via IAddChild.AddText.
throw new InvalidOperationException(SR.Format(SR.TextBoxInvalidChild, value.ToString()));
}
///<summary>
/// Called when text appears under the tag in markup.
///</summary>
///<param name="text">
/// Text to Add to the Object
///</param>
void IAddChild.AddText(string text)
{
ArgumentNullException.ThrowIfNull(text);
this.TextContainer.End.InsertTextInRun(text);
}
/// <summary>
/// Select the text in the given position and length.
/// </summary>View on GitHub (pinned to 81131a70a4)