dotnet/wpf · error · NotSupportedException
SR.TextBoxInvalidTextContainer
Error message
SR.TextBoxInvalidTextContainer
What it means
When the element registered as the TextBox content host turns out not to be a usable text container (it is not a valid render scope such as ScrollViewer/Decorator/Panel as expected), TextBoxBase nulls out the host and throws NotSupportedException. Designers require a no-content scenario to be tolerated, hence the nulling before the throw.
Solutions
- Make PART_ContentHost a supported type - ScrollViewer for TextBox, or a Decorator/Panel as the template expects.
- Restore the default part structure from the framework's default template.
- If a no-content design is intentional, ensure no element is incorrectly named as the content host so designers can render without one.
Example fix
// before <ContentPresenter x:Name="PART_ContentHost"/> <!-- unsupported type -> throws --> // after <ScrollViewer x:Name="PART_ContentHost"/>
Defensive patterns
Strategy: validation
Validate before calling
var host = template.FindName("PART_ContentHost", textBox) as FrameworkElement;
bool isValid = host is ScrollViewer || host is Decorator || host is Panel; Type guard
static bool IsSupportedContentHost(FrameworkElement fe) => fe is ScrollViewer || fe is Decorator || fe is Panel;
Try / catch
try
{
textBox.ApplyTemplate();
}
catch (NotSupportedException)
{
textBox.Template = (ControlTemplate)Application.Current.Resources["DefaultTextBoxTemplate"];
} Prevention
- Use ScrollViewer/Decorator/Panel for PART_ContentHost
- Do not rename or retype framework template parts
- Test custom templates in design and runtime scenarios
When it happens
Trigger: Applying a template whose PART_ContentHost element is of an unsupported type (e.g. a raw ContentPresenter or custom FrameworkElement instead of ScrollViewer/Decorator/Panel), so the created text container is invalid.
Common situations: Custom control templates that rename or retype the content host; migrating templates across WPF versions where part requirements changed.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- SR.TextBoxScrollViewerMarkedAsTextBoxContentMustHaveNoConten…
- SR.TextBoxDecoratorMarkedAsTextBoxContentMustHaveNoContent
- SR.FlowDocumentReaderDecoratorMarkedAsContentHostMustHaveNoC…
- SR.DocumentViewerStyleMustIncludeContentHost
- By default, ToolTip property does not support ToolTip…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/e1c4d26c30456b84.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/TextBoxBase.cs:1934
}
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 (_textBoxContentHost != null)
{
_textBoxContentHost = null;
// Remove the exception
throw new NotSupportedException(SR.TextBoxInvalidTextContainer);
}
}
// Attach render scope to TextEditor
InitializeRenderScope();
}
private void ClearContentHost()
{
// Detach render scope from TextEditor
UninitializeRenderScope();
// Render scope has been created by us,
// so we need to extract if from visual tree.
if (_textBoxContentHost is ScrollViewer)
{
((ScrollViewer)_textBoxContentHost).Content = null;
}View on GitHub (pinned to 81131a70a4)