{"record":{"id":"a4468a477cf19e8f","repo":"dotnet/wpf","slug":"page-can-have-only-one-child","errorCode":null,"errorMessage":"Page can have only one child.","messagePattern":"Page can have only one child\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Page.cs","lineNumber":82,"sourceCode":"        #endregion Constructors\n           \n        #region IAddChild\n        /// <summary>\n        ///     Adds a child. This is called by the parser\n        /// </summary>\n        /// <param name=\"obj\"></param>\n        void IAddChild.AddChild(Object obj)\n        {\n             VerifyAccess();\n\n             // if content is the first child or being cleared, set directly\n             if (Content == null || obj == null)\n             {\n                 Content = obj;\n             }\n             else\n             {\n                 throw new InvalidOperationException(SR.PageCannotHaveMultipleContent);\n             }             \n        }\n\n        ///<summary>\n        ///     This method is called by the parser when text appears under the tag in markup.\n        ///     By default Page does not support text; calling this method has no effect.\n        ///</summary>\n        ///<param name=\"str\">\n        ///     Text to add as a child.\n        ///</param>\n        void IAddChild.AddText (string str)\n        {            \n            XamlSerializerUtil.ThrowIfNonWhiteSpaceInAddText(str, this);\n        }\n        #endregion IAddChild\n\n        #region LogicalTree\n        /// <summary>","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Page.cs#L64-L100","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Assign page.Content = newChild (replaces existing content) instead of AddChild.","If multiple elements are needed, wrap them in a single root panel (Grid/StackPanel) and set that as Content.","Guard with `if (page.Content == null) page.AddChild(obj); else page.Content = obj;`."],"exampleFix":"// before\npage.AddChild(new TextBlock(\"a\"));\npage.AddChild(new Button()); // throws\n\n// after\nvar root = new StackPanel();\nroot.Children.Add(new TextBlock(\"a\"));\nroot.Children.Add(new Button());\npage.Content = root;","handlingStrategy":"validation","validationCode":"if (page.Content != null) { /* wrap or replace */ page.Content = newChild; } else { page.AddChild(newChild); }","typeGuard":null,"tryCatchPattern":"try { page.AddChild(obj); }\ncatch (InvalidOperationException) { page.Content = obj; }","preventionTips":["Use page.Content = ... instead of AddChild in code","Combine multiple children under one root panel","Remember XAML content already sets Content before code runs"],"tags":["wpf","page","content-model","invalidoperationexception"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}