dotnet/wpf · error

Cannot read Page properties because it is not in a tree…

Error message

Cannot read Page properties because it is not in a tree with Window as its root.

What it means

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.

Solutions

  1. Read WindowTitle only after the Page is loaded into a Window (e.g., in the Loaded event or after NavigationWindow navigation completes).
  2. Check the hosting first: only query when the Page is the Content of a Window (NavigationWindow).
  3. Store your own title string in a view-model/field instead of reading WindowService-backed properties.

Example fix

// before
public MyPage() { Title = this.WindowTitle; } // not in a Window tree yet

// after
Loaded += (s, e) => { if (Window.GetWindow(this) != null) Title = WindowTitle; };
Defensive patterns

Strategy: validation

Validate before calling

if (page.WindowService == null || Window.GetWindow(page) == null)
    throw new InvalidOperationException("Page must be hosted in a Window before reading WindowTitle.");

Type guard

bool CanQueryWindowProps(System.Windows.Controls.Page p) => Window.GetWindow(p) is NavigationWindow or Window;

Try / catch

try { title = page.WindowTitle; }
catch (InvalidOperationException) { title = fallbackTitle; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/0ef4b49d61bf702e. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Page.cs:158

                VerifyAccess();
                SetValue(ContentProperty, value);
            }
        }

        // All these properties are implemented
        // as "bound" to the window service.  For 
        // example, getting WindowTitle will return Window.Title,
        // setting WindowTitle will set Window.Title. 
        //
       
        string IWindowService.Title
        { 
            get
            {
                VerifyAccess();
                if (WindowService == null)
                {
                    throw new InvalidOperationException(SR.CannotQueryPropertiesWhenPageNotInTreeWithWindow);
                }
                return WindowService.Title;
            }
            
            set
            {
                VerifyAccess();
                if (WindowService == null)
                {
                    PageHelperObject._windowTitle = value;
                    PropertyIsSet(SetPropertyFlags.WindowTitle);
                }
                else if (_isTopLevel) // only top level page can set this property
                {
                    WindowService.Title = value;
                    PropertyIsSet(SetPropertyFlags.WindowTitle);
                }
            }

View on GitHub (pinned to 81131a70a4)