dotnet/wpf · error · NotSupportedException

SR.TextBoxDecoratorMarkedAsTextBoxContentMustHaveNoContent

Error message

SR.TextBoxDecoratorMarkedAsTextBoxContentMustHaveNoContent

What it means

Same contract as the ScrollViewer case but for a Decorator part (e.g. a Border) marked as the TextBox content host: if Decorator.Child is already non-null, TextBoxBase cannot insert its render scope and throws NotSupportedException. The content-host Decorator must be empty at template application time.

Solutions

  1. Remove the Decorator's Child and let TextBoxBase set it; place watermarks or overlays as siblings in the parent panel.
  2. Verify the content-host part has the correct part name and contains no children in the template.
  3. Start from the default template (copy via Expression Blend / template extraction) and modify only visuals.

Example fix

// before
<Border>
    <TextBlock Text="placeholder"/> <!-- Decorator.Child set -> throws -->
</Border>
// after
<Grid>
    <Border/>
    <TextBlock Text="placeholder" IsHitTestVisible="False"/>
</Grid>
Defensive patterns

Strategy: validation

Validate before calling

bool decoratorIsEmpty = decoratorPart == null || decoratorPart.Child == null;
if (decoratorIsEmpty) textBox.ApplyTemplate(); // safe to apply template with empty content-host Decorator

Type guard

static bool IsValidDecoratorHost(Decorator d) => d != null && d.Child == null;

Try / catch

try
{
    textBox.ApplyTemplate();
}
catch (NotSupportedException)
{
    // template misdeclares content host; log template name and fall back to default
}

Prevention

When it happens

Trigger: Custom TextBox ControlTemplate whose content-host Decorator (e.g. a Border) already has a Child element set in XAML or code before the template is applied.

Common situations: Templates that wrap the content host in a Border and also put placeholder/label text inside that same Border instead of a sibling element.

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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/53c61beae7e0afa7. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/TextBoxBase.cs:1915

                {
                    _renderScope = null;
                    _textBoxContentHost = null;
                    //  Do not throw exception
                    throw new NotSupportedException(SR.TextBoxScrollViewerMarkedAsTextBoxContentMustHaveNoContent);
                }
                else
                {
                    scrollViewer.Content = _renderScope; // this may replace old render scope in case of upgrade scenario in TextBox
                }
            }
            else if (_textBoxContentHost is Decorator decorator)
            {
                if (decorator.Child != null)
                {
                    _renderScope = null;
                    _textBoxContentHost = null;
                    //  Do not throw exception
                    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 (_textBoxContentHost != null)
                {
                    _textBoxContentHost = null;
                    //  Remove the exception

View on GitHub (pinned to 81131a70a4)