{"record":{"id":"d67e09eb4093b4cc","repo":"lepoco/wpfui","slug":"window-is-null","errorCode":null,"errorMessage":"Window is null","messagePattern":"Window is null","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Wpf.Ui/Controls/TitleBar/TitleBar.cs","lineNumber":471,"sourceCode":"\n    /// <inheritdoc />\n    protected override void OnInitialized(EventArgs e)\n    {\n        base.OnInitialized(e);\n\n        SetCurrentValue(ApplicationThemeProperty, Appearance.ApplicationThemeManager.GetAppTheme());\n        Appearance.ApplicationThemeManager.Changed += OnThemeChanged;\n    }\n\n    protected virtual void OnLoaded(object sender, RoutedEventArgs e)\n    {\n        if (DesignerHelper.IsInDesignMode)\n        {\n            return;\n        }\n\n        _currentWindow =\n            System.Windows.Window.GetWindow(this) ?? throw new InvalidOperationException(\"Window is null\");\n        if (_currentWindow.WindowState == WindowState.Maximized)\n        {\n            SetCurrentValue(IsMaximizedProperty, true);\n            _currentWindow.SetCurrentValue(Window.WindowStateProperty, WindowState.Maximized);\n        }\n\n        _currentWindow.StateChanged += OnParentWindowStateChanged;\n        _currentWindow.ContentRendered += OnWindowContentRendered;\n\n        SubscribeToSystemParameters();\n    }\n\n    private void OnUnloaded(object sender, RoutedEventArgs e)\n    {\n        Loaded -= OnLoaded;\n        Unloaded -= OnUnloaded;\n\n        Appearance.ApplicationThemeManager.Changed -= OnThemeChanged;","sourceCodeStart":453,"sourceCodeEnd":489,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/Controls/TitleBar/TitleBar.cs#L453-L489","documentation":"In OnLoaded, TitleBar calls Window.GetWindow(this) to capture its owning Window. GetWindow returns null when the TitleBar is not yet part of a live Window's visual tree (it is detached, hosted in a popup, or used outside a Window). Because TitleBar subscribes to the window's StateChanged and ContentRendered events, it cannot continue without a valid parent, so the null result is fatal.","triggerScenarios":"TitleBar is placed inside a UserControl/ContentControl that is not hosted in a Window at Loaded time; instantiated in a Popup, ContextMenu, or tooltip; Loaded fires during a transition where the visual parent is still null; used in a unit test or design host without a Window.","commonSituations":"Dragging a TitleBar into a non-Window container in the designer; re-parenting the TitleBar at runtime; using FluentWindow incorrectly such that the title bar is loaded before the window; test harnesses that instantiate controls without a Window.","solutions":["Host the TitleBar inside a Window (preferably FluentWindow) so GetWindow resolves at Loaded.","Delay or avoid placing TitleBar in UserControls/Popups that are not rooted in a Window.","If you re-parent at runtime, unload and reload the TitleBar after it is attached to the Window.","Guard your own code so the TitleBar is only created once it is in the window's visual tree."],"exampleFix":"// before\nvar bar = new TitleBar();\nsomePanel.Children.Add(bar); // panel not in a window yet -> throws on Loaded\n\n// after\nvar window = new FluentWindow();\nwindow.Content = bar; // bar is rooted in a window before Loaded fires","handlingStrategy":"validation","validationCode":"if (Window.GetWindow(titleBar) is null)\n{\n    throw new InvalidOperationException(\"TitleBar must be hosted in a Window before use.\");\n}","typeGuard":"static bool IsInWindow(FrameworkElement e) => Window.GetWindow(e) is not null;","tryCatchPattern":"try { titleBar.RaiseEvent(new RoutedEventArgs(FrameworkElement.LoadedEvent)); }\ncatch (InvalidOperationException ex) when (ex.Message == \"Window is null\")\n{\n    _logger.LogError(ex, \"TitleBar loaded outside a Window\");\n}","preventionTips":["Place TitleBar only inside Window/FluentWindow.","Do not instantiate TitleBar in Popups, tooltips, or detached panels.","In tests, host the control inside a Window and call Show() before driving it."],"tags":["titlebar","window","visual-tree","wpf"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}