{"record":{"id":"0ef4b49d61bf702e","repo":"dotnet/wpf","slug":"cannot-read-page-properties-because-it-is-not-in-a-tree-with","errorCode":null,"errorMessage":"Cannot read Page properties because it is not in a tree with Window as its root.","messagePattern":"Cannot read Page properties because it is not in a tree with Window as its root\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Page.cs","lineNumber":158,"sourceCode":"                VerifyAccess();\n                SetValue(ContentProperty, value);\n            }\n        }\n\n        // All these properties are implemented\n        // as \"bound\" to the window service.  For \n        // example, getting WindowTitle will return Window.Title,\n        // setting WindowTitle will set Window.Title. \n        //\n       \n        string IWindowService.Title\n        { \n            get\n            {\n                VerifyAccess();\n                if (WindowService == null)\n                {\n                    throw new InvalidOperationException(SR.CannotQueryPropertiesWhenPageNotInTreeWithWindow);\n                }\n                return WindowService.Title;\n            }\n            \n            set\n            {\n                VerifyAccess();\n                if (WindowService == null)\n                {\n                    PageHelperObject._windowTitle = value;\n                    PropertyIsSet(SetPropertyFlags.WindowTitle);\n                }\n                else if (_isTopLevel) // only top level page can set this property\n                {\n                    WindowService.Title = value;\n                    PropertyIsSet(SetPropertyFlags.WindowTitle);\n                }\n            }","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Page.cs#L140-L176","documentation":"Page.WindowTitle (Page.cs:158) requires the Page to be hosted in a tree rooted at Window (via WindowService); when the Page is not yet attached to such a window, reading it throws InvalidOperationException(SR.CannotQueryPropertiesWhenPageNotInTreeWithWindow). Xbap/host-agnostic pages have no WindowService until hosted.","triggerScenarios":"Reading page.WindowTitle (or setting it via the setter path) while the Page is not the content of a Window — e.g., before navigation completes, in a constructor, or when hosted outside a Window.","commonSituations":"Accessing WindowTitle in the Page constructor or Loaded handler before host attachment; using Page in a Frame or standalone; unit tests instantiating Page without a Window host.","solutions":["Read WindowTitle only after the Page is loaded into a Window (e.g., in the Loaded event or after NavigationWindow navigation completes).","Check the hosting first: only query when the Page is the Content of a Window (NavigationWindow).","Store your own title string in a view-model/field instead of reading WindowService-backed properties."],"exampleFix":"// before\npublic MyPage() { Title = this.WindowTitle; } // not in a Window tree yet\n\n// after\nLoaded += (s, e) => { if (Window.GetWindow(this) != null) Title = WindowTitle; };","handlingStrategy":"validation","validationCode":"if (page.WindowService == null || Window.GetWindow(page) == null)\n    throw new InvalidOperationException(\"Page must be hosted in a Window before reading WindowTitle.\");","typeGuard":"bool CanQueryWindowProps(System.Windows.Controls.Page p) => Window.GetWindow(p) is NavigationWindow or Window;","tryCatchPattern":"try { title = page.WindowTitle; }\ncatch (InvalidOperationException) { title = fallbackTitle; }","preventionTips":["Read WindowTitle/WindowWidth/WindowHeight only in Loaded or later","Check Window.GetWindow(this) != null first","Keep titles in your own state instead of the WindowService-backed property"],"tags":["wpf","page","navigation","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"}