{"record":{"id":"3a08890a1f38e188","repo":"dotnet/wpf","slug":"sr-noaddchild","errorCode":null,"errorMessage":"SR.NoAddChild","messagePattern":"SR\\.NoAddChild","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Navigation/NavigationWindow.cs","lineNumber":838,"sourceCode":"        //\n        //  Protected Methods\n        //\n        //------------------------------------------------------\n        #region Protected Methods\n        /// <summary>\n        /// Creates AutomationPeer (<see cref=\"UIElement.OnCreateAutomationPeer\"/>)\n        /// </summary>\n        protected override AutomationPeer OnCreateAutomationPeer()\n        {\n            return new NavigationWindowAutomationPeer(this);\n        }\n\n        /// <summary>\n        ///  Add an object child to this control\n        /// </summary>\n        protected override void AddChild(object value)\n        {\n            throw new InvalidOperationException(SR.NoAddChild);\n        }\n\n        /// <summary>\n        ///  Add a text string to this control\n        /// </summary>\n        protected override void AddText(string text)\n        {\n            XamlSerializerUtil.ThrowIfNonWhiteSpaceInAddText(text, this);\n        }\n\n        /// <summary>\n        ///     This even fires when window is closed. This event is non cancelable and is\n        ///     for user infromational purposes\n        /// </summary>\n        /// <remarks>\n        ///     This method follows the .Net programming guideline of having a protected virtual\n        ///     method that raises an event, to provide a convenience for developers that subclass\n        ///     the event. If you override this method - you need to call Base.OnClosed(...) for","sourceCodeStart":820,"sourceCodeEnd":856,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Navigation/NavigationWindow.cs#L820-L856","documentation":"NavigationWindow overrides AddChild to explicitly forbid adding object children through the XAML content-model, since its content must be set via the Content property / navigation. Any attempt to append a child (e.g. XAML '<NavigationWindow><Button/>') throws.","triggerScenarios":"Calling navigationWindow.AddChild(obj) directly, or declaring child elements inside a NavigationWindow in XAML, which routes through AddChild during parsing.","commonSituations":"Migrating from Window to NavigationWindow while keeping child-element XAML; programmatic UI construction code calling AddChild on a NavigationWindow.","solutions":["Set NavigationWindow.Content to a single element instead of adding children.","Put your UI in a Page and navigate to it (Source or Navigate()).","Wrap multiple elements in a container (Grid/StackPanel) assigned to Content.","Remove child elements from NavigationWindow XAML and host them in the navigated page."],"exampleFix":"// before\n<NavigationWindow ...>\n  <Button Content=\"OK\"/>\n</NavigationWindow>\n// after\n<NavigationWindow x:Name=\"navWin\" Source=\"MainPage.xaml\" ... />\n// or navWin.Content = new Button{ Content=\"OK\" };","handlingStrategy":"type-guard","validationCode":"if (navWindow is NavigationWindow) { navWindow.Content = element; } else { navWindow.AddChild(element); }","typeGuard":"void SetContent(Window w, object c) { if (w is NavigationWindow) ((NavigationWindow)w).Content = c; else w.Content = c; }","tryCatchPattern":"try { navWindow.Content = element; }\ncatch (InvalidOperationException) { /* fall back to Page navigation */ }","preventionTips":["Never nest elements directly under <NavigationWindow> in XAML.","Always use Content or Navigate()/Source for NavigationWindow UI.","Replace AddChild/AddText calls when migrating from Window to NavigationWindow.","Wrap multi-element UIs in a single panel assigned to Content."],"tags":["wpf","navigation","xaml","content-model"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}