dotnet/wpf · error
Decorator marked as PART_ContentHost must have no content.
Error message
Decorator marked as PART_ContentHost must have no content.
What it means
The same template-application logic supports a Decorator (e.g. Border) as PART_ContentHost. If the Decorator's Child is already set, WPF throws NotSupportedException — the decorator must be empty because the internal render scope will be installed as its Child.
Solutions
- Leave the PART_ContentHost Decorator empty (no Child in XAML)
- Move any visual adjustments to the Decorator's own properties (Padding, Background, CornerRadius)
- Wrap with an outer element for extra visuals, keeping the named decorator childless
- Start from the default PasswordBox template and modify only outer chrome
Example fix
// before <Border x:Name="PART_ContentHost"> <TextBlock Text="inner"/> <!-- throws --> </Border> // after <Border x:Name="PART_ContentHost" Padding="4" Background="White"/>
Defensive patterns
Strategy: validation
Validate before calling
var dec = templateChild as Decorator; bool ok = dec != null && dec.Child == null; // PART_ContentHost decorator must be empty
Type guard
bool isEmptyDecoratorHost(FrameworkElement fe) => fe is Decorator d && d.Child == null;
Prevention
- Never nest children inside the PART_ContentHost Decorator
- Use Decorator properties (Padding, Background) for styling instead of nested content
- Keep the content-host element as the single child of its container
- Diff custom templates against the default one after edits
When it happens
Trigger: Custom PasswordBox template where PART_ContentHost is a Border/Decorator with a child element inside it, evaluated when the template is applied and InitializeRenderScope runs.
Common situations: Decorating the content host with padding/background by nesting content inside the Border; adapting TextBox or ComboBox templates that keep children inside the content-host decorator.
Understand the failure class
Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.
Related errors
- Only Decorator and ScrollViewer can be used as…
- ScrollViewer marked as PART_ContentHost must have no…
- ElementNotEnabledException
- InvalidOperationException (no message)
- InvalidOperationException
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/de4bb80e6e7ef3bf.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/PasswordBox.cs:951
// Add renderScope as a child of ContentHostTemplateName
_renderScope = renderScope;
if (_passwordBoxContentHost is ScrollViewer scrollViewer)
{
if (scrollViewer.Content != null)
{
throw new NotSupportedException(SR.TextBoxScrollViewerMarkedAsTextBoxContentMustHaveNoContent);
}
else
{
scrollViewer.Content = _renderScope;
}
}
else if (_passwordBoxContentHost is Decorator decorator)
{
if (decorator.Child != null)
{
throw new NotSupportedException(SR.TextBoxDecoratorMarkedAsTextBoxContentMustHaveNoContent);
}
else
{
decorator.Child = _renderScope; // this may replace old render scope in case of upgrade scenario in TextBox
}
}
else
{
// When we implement TextContainer setting via TextView interface
// all text containing element will become allowed here.
_renderScope = null;
// Explicitly not throwing an exception here when content host = null
// -- designers need us to support no content scenarios
if (_passwordBoxContentHost != null)
{
_passwordBoxContentHost = null;
// Remove the exceptionView on GitHub (pinned to 81131a70a4)