{"record":{"id":"7e05219e053bbbf4","repo":"dotnet/wpf","slug":"sr-format-sr-wrongnavigaterootelement-bp-tostring","errorCode":null,"errorMessage":"SR.Format(SR.WrongNavigateRootElement, bp.ToString())","messagePattern":"SR\\.Format\\(SR\\.WrongNavigateRootElement, bp\\.ToString\\(\\)\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Navigation/NavigationService.cs","lineNumber":1102,"sourceCode":"        /// The callback that happens when the bind product corresponding to a\n        /// URI has been created.\n        /// </summary>\n        /// <param name=\"contentType\">MIME type from which product was created</param>\n        /// <param name=\"bp\">Content created.</param>\n        /// <param name=\"bpu\">Absolute URI of content, or null</param>\n        /// <param name=\"navState\"></param>\n        void IContentContainer.OnContentReady(ContentType contentType, Object bp, Uri bpu, Object navState)\n        {\n            Invariant.Assert(bpu == null || bpu.IsAbsoluteUri, \"Content URI must be absolute.\");\n            if (IsDisposed)\n            {\n                return;\n            }\n\n            // If an invalid root element is passed to Navigation Service, throw exception here.\n            if (!IsValidRootElement(bp))\n            {\n                throw new InvalidOperationException(SR.Format(SR.WrongNavigateRootElement, bp.ToString()));\n            }\n\n            /*TODO: Uncomment after new loader design is implemented or sync bind reentrancy is resolved\n            Debug.Assert(_navStatus == NavigationStatus.Navigating,\n                         \"Navigation State Machine is messed up, Expected: \" + NavigationStatus.Navigating + \"; Current: \" + _navStatus);*/\n\n            ResetPendingNavigationState(NavigationStatus.Navigated);\n\n            NavigateInfo navInfo = navState as NavigateInfo;\n            NavigationMode navMode = navInfo == null ? NavigationMode.New : navInfo.NavigationMode;\n\n            Debug.Assert(bpu == null ||\n                         navInfo == null ||\n                         navInfo.Source == null ||\n                         IsSameUri(null, navInfo.Source, bpu, false /* withFragment */),\n                         \"Source in OnContentReady does not match source in NavigateInfo\");\n            if (bpu == null)\n            {","sourceCodeStart":1084,"sourceCodeEnd":1120,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Navigation/NavigationService.cs#L1084-L1120","documentation":"When downloaded content arrives, NavigationService.OnContentReady() validates the root element via IsValidRootElement(); if the object is not a valid navigation root (e.g. not a Page, PageFunctionBase, or applicable content type) it throws InvalidOperationException with WrongNavigateRootElement including the object's string. NavigationService can only navigate to recognized root element types.","triggerScenarios":"Navigating to an object or XAML whose root element is not a valid root (e.g. a raw Panel/Control that isn't a Page, or non-UI object) so OnContentReady fails the IsValidRootElement check.","commonSituations":"Navigating a Frame directly to a UserControl/Panel instead of a Page; loading loose XAML whose root is a layout control; Navigate(object) with an arbitrary business object without a converter/DataTemplate-based page.","solutions":["Wrap the content in a Page (or use PageFunction) and navigate to that","Navigate to a XAML file whose root is <Page> rather than a bare control","For object navigation, navigate to a page and supply the object via DataContext or NavigationContext"],"exampleFix":"// before\nframe.Content = new Grid(); // or Navigate(grid)\n// after\nvar page = new Page { Content = new Grid() };\nframe.Navigate(page);","handlingStrategy":"validation","validationCode":"bool validRoot = content is Page || content is PageFunctionBase;\nif (!validRoot) content = new Page { Content = content };","typeGuard":"bool IsValidNavRoot(object c) => c is Page or PageFunctionBase;","tryCatchPattern":"try { frame.Navigate(content); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"root element\")) {\n    frame.Navigate(new Page { Content = content });\n}","preventionTips":["Always navigate to Page-derived content","Make loose-XAML roots <Page>, not bare panels","Route object data through a Page's DataContext instead of Navigate(object)"],"tags":["wpf","navigation","page","invalid-content-root"],"backgroundTag":"invalid-argument-value","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"}