dotnet/wpf · error · ArgumentException

SR.Format(SR.UnexpectedParameterType, value.GetType()…

Error message

SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(PageContent))

What it means

This ArgumentException is thrown by FixedDocument.IAddChild.AddChild when the value is not a PageContent. FixedDocument pages are represented solely by PageContent objects, so any other child type is rejected.

Solutions

  1. Wrap content in a PageContent element and add that to the document
  2. Use Pages.Add(pageContent) with a properly initialized PageContent
  3. In XAML use <PageContent><FixedPage>...</FixedPage></PageContent>
  4. Check the offending type in the message

Example fix

// before
fixedDocument.AddChild(new FixedPage());
// after
var pc = new PageContent();
((IAddChild)pc).AddChild(new FixedPage());
fixedDocument.Pages.Add(pc);
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is not PageContent pc) throw new ArgumentException("FixedDocument children must be PageContent");

Type guard

bool IsPageContent(object o) => o is PageContent;

Try / catch

try { ((IAddChild)fixedDocument).AddChild(value); } catch (ArgumentException ex) { /* wrap value in PageContent and retry */ }

Prevention

When it happens

Trigger: Adding a child to FixedDocument via IAddChild (or XAML) that is not a PageContent — e.g. adding a FixedPage directly, or a UIElement.

Common situations: XAML that nests <FixedPage> directly under <FixedDocument> instead of wrapping it in <PageContent>; programmatic document construction with wrong child type.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedDocument.cs:136

        /// Called to Add the object as a Child.
        ///</summary>
        /// <exception cref="ArgumentNullException">value is NULL.</exception>
        /// <exception cref="ArgumentException">value is not of type PageContent.</exception>
        /// <exception cref="InvalidOperationException">A PageContent cannot be added while previous partial page isn't completely Loaded.</exception>
        ///<param name="value">
        /// Object to add as a child
        ///</param>
        void IAddChild.AddChild(Object value)
        {
            ArgumentNullException.ThrowIfNull(value);

//             Dispatcher.VerifyAccess();

            PageContent fp = value as PageContent;

            if (fp == null)
            {
                throw new ArgumentException(SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(PageContent)), nameof(value));
            }

            if (fp.IsInitialized)
            {
                _pages.Add(fp);
            }
            else
            {
                DocumentsTrace.FixedFormat.FixedDocument.Trace($"Page {_pages.Count} Deferred");
                if (_partialPage == null)
                {
                    _partialPage = fp;
                    _partialPage.ChangeLogicalParent(this);
                    _partialPage.Initialized += new EventHandler(OnPageLoaded);
                }
                else
                {
                    throw new InvalidOperationException(SR.PrevoiusPartialPageContentOutstanding);

View on GitHub (pinned to 81131a70a4)