dotnet/wpf · error · ArgumentException

SR.FlowDocumentReaderCanHaveOnlyOneChild

Error message

SR.FlowDocumentReaderCanHaveOnlyOneChild

What it means

FlowDocumentReader's explicit IAddChild.AddChild implementation throws this ArgumentException when a child is added in markup while Document has already been set. FlowDocumentReader accepts only one child, which must be a FlowDocument that becomes its Document.

Solutions

  1. Declare only one FlowDocument child inside FlowDocumentReader in XAML.
  2. Replace an existing document by assigning reader.Document = newDoc instead of adding another child.
  3. Remove leftover extra elements (panels, text) from inside the FlowDocumentReader tag; only a FlowDocument is allowed as the single child.

Example fix

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

// after
<FlowDocumentReader>
  <FlowDocument/>
</FlowDocumentReader>
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

static bool IsSingleFlowDocumentChild(FlowDocumentReader r, object child) => r.Document == null && child is FlowDocument;

Try / catch

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

Prevention

When it happens

Trigger: XAML/programmatic use of IAddChild.AddChild when FlowDocumentReader.Document (or a prior child) is already non-null, e.g. two children declared inside <FlowDocumentReader>.

Common situations: Accidentally nesting two FlowDocument elements or a FlowDocument plus another element inside the reader in XAML; code that reuses a reader instance and adds a second document child instead of assigning Document.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/FlowDocumentReader.cs:1917

        //
        //  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.FlowDocumentReaderCanHaveOnlyOneChild);
            }
            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)