dotnet/wpf · error · ArgumentException

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

Error message

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

What it means

PageContent.AddChild throws ArgumentException with SR.UnexpectedParameterType when the value passed is not a FixedPage. PageContent may only host a FixedPage child; any other object type is rejected before being attached.

Solutions

  1. Pass a FixedPage instance as the child value
  2. Wrap or convert the content into a FixedPage before adding
  3. Fix XAML so only <FixedPage> appears inside <PageContent>

Example fix

// before
pageContent.AddChild(new Canvas());
// after
FixedPage fp = new FixedPage();
fp.Children.Add(new Canvas());
pageContent.AddChild(fp);
Defensive patterns

Strategy: type-guard

Validate before calling

if (value == null) throw new ArgumentNullException(nameof(value));
if (value is not FixedPage) throw new ArgumentException("Expected FixedPage", nameof(value));

Type guard

bool IsValidChild(object v) => v is FixedPage;

Try / catch

try { pageContent.AddChild(value); } catch (ArgumentException ex) { /* log expected-FixedPage violation */ }

Prevention

When it happens

Trigger: Calling AddChild(obj) (or the IAddChild implementation) on a PageContent with an object that is not a FixedPage, e.g. a Page, Canvas, or deserialized element of a different type.

Common situations: XPS document construction where the wrong element type is inserted as page content; programmatic XAML building that assumes PageContent accepts arbitrary Visuals.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/PageContent.cs:171

        ///</summary>
        /// <exception cref="ArgumentNullException">value is NULL.</exception>
        /// <exception cref="ArgumentException">value is not of type FixedPage.</exception>
        /// <exception cref="InvalidOperationException">A child already exists (a PageContent can have at most one child).</exception>
        ///<param name="value">
        /// Object to add as a child
        ///</param>
        /// <ExternalAPI/>
        void IAddChild.AddChild(Object value)
        {
//             VerifyAccess();

            ArgumentNullException.ThrowIfNull(value);


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

            if (_child != null)
            {
                throw new InvalidOperationException(SR.Format(SR.CanOnlyHaveOneChild, typeof(PageContent), value));
            }

            _pageRef = null;
            _child   = fp;
            LogicalTreeHelper.AddLogicalChild(this, _child);
        }

        ///<summary>
        /// Called when text appears under the tag in markup
        ///</summary>
        ///<param name="text">
        /// Text to Add to the Object
        ///</param>

View on GitHub (pinned to 81131a70a4)