dotnet/wpf · error · ArgumentException

SR.UnexpectedParameterType

Error message

SR.UnexpectedParameterType

What it means

FlowDocumentReader's IAddChild.AddChild throws this ArgumentException (formatted with SR.UnexpectedParameterType) when the value passed is not a FlowDocument. The single child of a FlowDocumentReader must be a FlowDocument; any other element or object type is rejected with the actual and expected type names in the message.

Solutions

  1. Wrap content in a FlowDocument (e.g. FlowDocument containing Paragraphs) before making it the child of FlowDocumentReader.
  2. If you want arbitrary UI content, use a different container control instead of FlowDocumentReader.
  3. Type-check the value before calling AddChild programmatically: if (value is FlowDocument) reader.AddChild(value);

Example fix

// before
reader.AddChild(new Paragraph(new Run("hi"))); // throws

// after
var doc = new FlowDocument(new Paragraph(new Run("hi")));
reader.AddChild(doc); // or reader.Document = doc;
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is not FlowDocument doc)
    throw new ArgumentException($"Expected FlowDocument, got {value?.GetType().Name}");
reader.Document = doc;

Type guard

static bool IsFlowDocument(object v) => v is FlowDocument;

Try / catch

try { reader.AddChild(value); }
catch (ArgumentException) { // value was not a FlowDocument; wrap or reject }

Prevention

When it happens

Trigger: Calling AddChild with a non-FlowDocument value, or placing an element other than FlowDocument as the direct child of <FlowDocumentReader> in XAML.

Common situations: Putting a StackPanel, Paragraph, or other control directly under FlowDocumentReader instead of wrapping content in a FlowDocument; code-generated markup adding wrong-typed children.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

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

        #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);
        }

        #endregion IAddChild Members

        //-------------------------------------------------------------------
        //

View on GitHub (pinned to 81131a70a4)