dotnet/wpf · error · InvalidOperationException
SR.ContentControlCannotHaveMultipleContent
Error message
SR.ContentControlCannotHaveMultipleContent
What it means
ContentControl.AddChild throws when the control already has non-null Content and AddChild is called with another non-null object. A ContentControl can hold exactly one content element, so adding a second child directly via AddChild is not allowed. Use a panel or an items control if multiple children are needed.
Solutions
- Set the Content property instead of calling AddChild: control.Content = child (this replaces existing content).
- If multiple children are required, set Content to a StackPanel/Grid and add children to that panel.
- Use an ItemsControl-derived type (ListBox, ItemsControl) instead of ContentControl when the content is a collection.
- Null the Content first if the intent is to replace it, then AddChild.
Example fix
// before
button.Content = "Click";
button.AddChild(new Image()); // throws
// after
var panel = new StackPanel();
panel.Children.Add(new TextBlock { Text = "Click" });
panel.Children.Add(new Image());
button.Content = panel; Defensive patterns
Strategy: validation
Validate before calling
bool canAddChild = control.Content == null || child == null;
Type guard
static bool HasNoContent(ContentControl c) => c.Content == null;
Try / catch
try
{
control.AddChild(child);
}
catch (InvalidOperationException)
{
control.Content = child; // replace existing content instead
} Prevention
- Set Content directly instead of AddChild in code
- Use a layout panel as Content when multiple children are needed
- Check Content != null before AddChild
When it happens
Trigger: Calling contentControl.AddChild(child) twice, or calling AddChild on a control whose Content was already set in XAML or via the Content property.
Common situations: Building a Button/Window/Label in code and calling AddChild after setting Content, or migrating XAML where the control already had inline content into code that calls AddChild.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- Animation_NoTextChildren
- Page can have only one child.
- SR.NoAddChild
- SR.ParserCanOnlyHaveOneChild
- " }} " element found. Expected fixed page element ( }} ).
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/47f97db6d14566fe.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ContentControl.cs:195
/// </summary>
void IAddChild.AddChild(object value)
{
AddChild(value);
}
/// <summary>
/// Add an object child to this control
/// </summary>
protected virtual void AddChild(object value)
{
// if conent is the first child or being cleared, set directly
if (Content == null || value == null)
{
Content = value;
}
else
{
throw new InvalidOperationException(SR.ContentControlCannotHaveMultipleContent);
}
}
/// <summary>
/// Add a text string to this control
/// </summary>
void IAddChild.AddText(string text)
{
AddText(text);
}
/// <summary>
/// Add a text string to this control
/// </summary>
protected virtual void AddText(string text)
{
AddChild(text);
}View on GitHub (pinned to 81131a70a4)