dotnet/wpf · error · ArgumentException

SR.UnexpectedParameterType

Error message

SR.UnexpectedParameterType

What it means

FlowDocumentScrollViewer's IAddChild.AddChild throws this ArgumentException (formatted with SR.UnexpectedParameterType) when the supplied value is not a FlowDocument. The only permitted child is a FlowDocument; the message includes the provided and expected type names.

Solutions

  1. Wrap the content in a FlowDocument before adding it as the viewer's child.
  2. Use a different container control if you need arbitrary UI children rather than flow content.
  3. Type-check before adding: if (value is FlowDocument doc) viewer.Document = doc;

Example fix

// before
viewer.AddChild(new Run("text")); // throws

// after
viewer.AddChild(new FlowDocument(new Paragraph(new Run("text"))));
Defensive patterns

Strategy: type-guard

Validate before calling

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

Type guard

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

Try / catch

try { viewer.AddChild(value); }
catch (ArgumentException) { // wrap value in a FlowDocument and retry }

Prevention

When it happens

Trigger: Calling AddChild with a non-FlowDocument value, or placing an element other than FlowDocument directly inside <FlowDocumentScrollViewer> in XAML.

Common situations: Nesting Paragraph, Grid or other elements directly under the viewer instead of inside a FlowDocument; generating children programmatically with the wrong type.

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

Appendix: source

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

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

        #endregion IAddChild Members

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

View on GitHub (pinned to 81131a70a4)