dotnet/wpf · error · NotSupportedException

SR.FlowDocumentScrollViewerMarkedAsContentHostMustHaveNoCont…

Error message

SR.FlowDocumentScrollViewerMarkedAsContentHostMustHaveNoContent

What it means

FlowDocumentScrollViewer.OnApplyTemplate throws this NotSupportedException when the template part named as the content host (PART_ContentHost, a ScrollViewer) is found but already has Content set. The content host part is reserved for the viewer's internal document presenter; user-supplied content in it would break the viewer's scrolling/layout wiring.

Solutions

  1. Remove all content from the PART_ContentHost ScrollViewer in your ControlTemplate; keep it empty.
  2. If you need extra visuals, place them outside the content host part or in other template parts.
  3. Start from the default FlowDocumentScrollViewer template and only modify elements other than the content host.

Example fix

<!-- before: throws -->
<ScrollViewer x:Name="PART_ContentHost">
  <Border Background="Beige"/>
</ScrollViewer>

<!-- after -->
<ScrollViewer x:Name="PART_ContentHost"/>
<Border Background="Beige"/> <!-- moved outside the host -->
Defensive patterns

Strategy: validation

Validate before calling

// In your custom template review step:
var host = template.FindName("PART_ContentHost", templatedParent) as ScrollViewer;
if (host != null && host.Content != null)
    throw new InvalidOperationException("PART_ContentHost must have no content.");

Try / catch

try { viewer.ApplyTemplate(); }
catch (NotSupportedException ex) { logger.Error(ex); LoadDefaultTemplate(); }

Prevention

When it happens

Trigger: Applying a custom ControlTemplate for FlowDocumentScrollViewer whose PART_ContentHost ScrollViewer element has child content (e.g. extra elements inside the ScrollViewer).

Common situations: Custom restyling of FlowDocumentScrollViewer where developers add decorations or wrappers inside the content host part; copying a template from another control and leaving content in the part.

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


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/FlowDocumentScrollViewer.cs:124

            _toolBarHost?.Visibility = IsToolBarVisible ? Visibility.Visible : Visibility.Collapsed;

            // Initialize ContentHost.
            // If old ContentHost is enabled, disable it first to ensure appropriate cleanup.
            if (_contentHost != null)
            {
                BindingOperations.ClearBinding(_contentHost, HorizontalScrollBarVisibilityProperty);
                BindingOperations.ClearBinding(_contentHost, VerticalScrollBarVisibilityProperty);
                _contentHost.ScrollChanged -= new ScrollChangedEventHandler(OnScrollChanged);
                RenderScope.Document = null;
                ClearValue(TextEditor.PageHeightProperty);
                _contentHost.Content = null;
            }
            _contentHost = GetTemplateChild(_contentHostTemplateName) as ScrollViewer;
            if (_contentHost != null)
            {
                if (_contentHost.Content != null)
                {
                    throw new NotSupportedException(SR.FlowDocumentScrollViewerMarkedAsContentHostMustHaveNoContent);
                }
                _contentHost.ScrollChanged += new ScrollChangedEventHandler(OnScrollChanged);
                CreateTwoWayBinding(_contentHost, HorizontalScrollBarVisibilityProperty, "HorizontalScrollBarVisibility");
                CreateTwoWayBinding(_contentHost, VerticalScrollBarVisibilityProperty, "VerticalScrollBarVisibility");

                // Need to make ScrollViewer non-focusable, otherwise it will eat keyboard navigation from editor.
                _contentHost.Focusable = false;

                // Initialize the content of the ScrollViewer.
                _contentHost.Content = new FlowDocumentView();
                RenderScope.Document = Document;
            }

            // Initialize TextEditor.
            AttachTextEditor();

            // Apply the current zoom to the content host.
            ApplyZoom();

View on GitHub (pinned to 81131a70a4)