dotnet/wpf · error

Page can have only one child.

Error message

Page can have only one child.

What it means

Page.AddChild throws InvalidOperationException(SR.PageCannotHaveMultipleContent) because Page has a single Content property. AddChild can only be used when Content is currently null; a second call (or mixing AddChild with XAML content or Content assignment) attempts to give the Page two children.

Solutions

  1. Assign page.Content = newChild (replaces existing content) instead of AddChild.
  2. If multiple elements are needed, wrap them in a single root panel (Grid/StackPanel) and set that as Content.
  3. Guard with `if (page.Content == null) page.AddChild(obj); else page.Content = obj;`.

Example fix

// before
page.AddChild(new TextBlock("a"));
page.AddChild(new Button()); // throws

// after
var root = new StackPanel();
root.Children.Add(new TextBlock("a"));
root.Children.Add(new Button());
page.Content = root;
Defensive patterns

Strategy: validation

Validate before calling

if (page.Content != null) { /* wrap or replace */ page.Content = newChild; } else { page.AddChild(newChild); }

Try / catch

try { page.AddChild(obj); }
catch (InvalidOperationException) { page.Content = obj; }

Prevention

When it happens

Trigger: Calling page.AddChild(obj) when the Page already has Content set (via XAML markup, Content= assignment, or a previous AddChild), including parser-driven AddChild during load.

Common situations: Building a Page in code after loading XAML that already declares a root child; calling AddChild in a loop; migrating Window-style multi-child code to Page.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Page.cs:82

        #endregion Constructors
           
        #region IAddChild
        /// <summary>
        ///     Adds a child. This is called by the parser
        /// </summary>
        /// <param name="obj"></param>
        void IAddChild.AddChild(Object obj)
        {
             VerifyAccess();

             // if content is the first child or being cleared, set directly
             if (Content == null || obj == null)
             {
                 Content = obj;
             }
             else
             {
                 throw new InvalidOperationException(SR.PageCannotHaveMultipleContent);
             }             
        }

        ///<summary>
        ///     This method is called by the parser when text appears under the tag in markup.
        ///     By default Page does not support text; calling this method has no effect.
        ///</summary>
        ///<param name="str">
        ///     Text to add as a child.
        ///</param>
        void IAddChild.AddText (string str)
        {            
            XamlSerializerUtil.ThrowIfNonWhiteSpaceInAddText(str, this);
        }
        #endregion IAddChild

        #region LogicalTree
        /// <summary>

View on GitHub (pinned to 81131a70a4)