dotnet/wpf · error · ArgumentException

SR.FlowDocumentScrollViewerCanHaveOnlyOneChild

Error message

SR.FlowDocumentScrollViewerCanHaveOnlyOneChild

What it means

FlowDocumentScrollViewer's explicit IAddChild.AddChild implementation throws this ArgumentException when a child is added while Document has already been set. The viewer accepts exactly one child, a FlowDocument, which becomes its Document.

Solutions

  1. Declare only a single FlowDocument child inside FlowDocumentScrollViewer in XAML.
  2. Assign viewer.Document = newDocument to replace content instead of calling AddChild again.
  3. Remove any non-FlowDocument or extra elements from inside the viewer tag.

Example fix

// before (XAML)
<FlowDocumentScrollViewer>
  <FlowDocument/><FlowDocument/> <!-- throws -->
</FlowDocumentScrollViewer>

// after
<FlowDocumentScrollViewer>
  <FlowDocument/>
</FlowDocumentScrollViewer>
Defensive patterns

Strategy: validation

Validate before calling

if (viewer.Document != null)
{
    viewer.Document = newDoc; // replace, do not add
}

Type guard

static bool CanAddChild(FlowDocumentScrollViewer v, object child) => v.Document == null && child is FlowDocument;

Try / catch

try { viewer.AddChild(value); }
catch (ArgumentException) { viewer.Document = value as FlowDocument; }

Prevention

When it happens

Trigger: Adding a second child (XAML element inside <FlowDocumentScrollViewer> or a programmatic AddChild call) when Document is already assigned.

Common situations: Two children declared inside the viewer tag in XAML; re-adding a document instead of reassigning the Document property; leftover elements inside the viewer.

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/eb6852a7ab8bc993. Report an issue: GitHub.

Appendix: source

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

        //
        //  IAddChild Members
        //
        //-------------------------------------------------------------------

        #region IAddChild Members

        /// <summary>
        /// Called to add the object as a Child.
        /// </summary>
        /// <param name="value">Object to add as a child.</param>
        /// <remarks>FlowDocumentScrollViewer only supports a single child of type IDocumentPaginator.</remarks>
        void IAddChild.AddChild(Object value)
        {
            ArgumentNullException.ThrowIfNull(value);
            // Check if Content has already been set.
            if (this.Document != null)
            {
                throw new ArgumentException(SR.FlowDocumentScrollViewerCanHaveOnlyOneChild);
            }
            if (!(value is FlowDocument))
            {
                throw new ArgumentException(SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(FlowDocument)), nameof(value));
            }
            Document = value as FlowDocument;
        }

        /// <summary>
        /// Called when text appears under the tag in markup
        /// </summary>
        /// <param name="text">Text to add to the Object.</param>
        /// <remarks>FlowDocumentScrollViewer does not support Text children.</remarks>
        void IAddChild.AddText(string text)
        {
            XamlSerializerUtil.ThrowIfNonWhiteSpaceInAddText(text, this);
        }

View on GitHub (pinned to 81131a70a4)