{"record":{"id":"789094a4219d8ec4","repo":"dotnet/wpf","slug":"page-can-have-only-window-or-frame-as-parent","errorCode":null,"errorMessage":"Page can have only Window or Frame as parent.","messagePattern":"Page can have only Window or Frame as parent\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Page.cs","lineNumber":697,"sourceCode":"                    // tree of an element)\n\n                    // We need this here for the case when Frame is in the style \n                    // of an element\n                    if (feParent is Frame)\n                    {\n                        break;\n                    }\n                }\n\n                if ((parent is Window) || (parent is Frame))\n                {\n                    isParentValid = true;\n                }\n            }\n\n            if (!isParentValid)\n            {\n                throw new InvalidOperationException(SR.ParentOfPageMustBeWindowOrFrame);\n            }\n        }\n\n        #endregion Protected Methods\n                \n        //---------------------------------------------------\n        //\n        // Private Methods\n        //\n        //---------------------------------------------------\n        #region Private Methods\n\n        private static void OnContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)\n        {\n            Page page = (Page) d;\n            page.OnContentChanged(e.OldValue, e.NewValue);\n        }\n","sourceCodeStart":679,"sourceCodeEnd":715,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Page.cs#L679-L715","documentation":"During OnVisualParentChanged, Page validates that its new visual parent is a Window or Frame. Any other parent (ContentControl, Grid, arbitrary panel) is rejected with this InvalidOperationException because Page relies on WindowService infrastructure only those hosts provide.","triggerScenarios":"Putting a Page directly as the Content of a Window's inner element (e.g. <Grid><Page/></Grid>), adding a Page to a StackPanel, Border, ContentControl, or any non-Window/non-Frame container.","commonSituations":"Developers treating Page like a UserControl and embedding it in layout panels; migrating navigation code and hosting pages in ContentControl; design-time previews placing Page in arbitrary containers.","solutions":["Host the Page in a Frame: <Frame Source=\"MyPage.xaml\"/> or frame.Content = new MyPage()","Show the Page in a NavigationWindow for top-level navigation hosting","Convert the Page to a UserControl if you only want it embedded in ordinary layout","Never add Page as a child of panels/ContentControl in XAML"],"exampleFix":"// before\n<Window>\n  <Grid>\n    <local:MyPage/> <!-- throws -->\n  </Grid>\n</Window>\n// after\n<Window>\n  <Frame Source=\"MyPage.xaml\"/>\n</Window>","handlingStrategy":"validation","validationCode":"// XAML-time check: Page may only appear under Window/Frame\nbool validHost = page.Parent is Window || page.Parent is Frame;\nif (!validHost) throw new InvalidOperationException(\"Host Page with Frame or NavigationWindow\");","typeGuard":"bool isValidPageHost(object parent) => parent is Window || parent is Frame;","tryCatchPattern":null,"preventionTips":["Always host pages with <Frame> or NavigationWindow in XAML","Use UserControl instead of Page for embeddable content","Never place <Page/> inside Grid/StackPanel/ContentControl","Search templates for stray Page elements after refactors"],"tags":["wpf","page","visual-tree"],"backgroundTag":"invalid-parent-element","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"}