lepoco/wpfui · error · InvalidOperationException
Window is null
Error message
Window is null
What it means
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.
Source
Thrown at src/Wpf.Ui/Controls/TitleBar/TitleBar.cs:471
/// <inheritdoc />
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
SetCurrentValue(ApplicationThemeProperty, Appearance.ApplicationThemeManager.GetAppTheme());
Appearance.ApplicationThemeManager.Changed += OnThemeChanged;
}
protected virtual void OnLoaded(object sender, RoutedEventArgs e)
{
if (DesignerHelper.IsInDesignMode)
{
return;
}
_currentWindow =
System.Windows.Window.GetWindow(this) ?? throw new InvalidOperationException("Window is null");
if (_currentWindow.WindowState == WindowState.Maximized)
{
SetCurrentValue(IsMaximizedProperty, true);
_currentWindow.SetCurrentValue(Window.WindowStateProperty, WindowState.Maximized);
}
_currentWindow.StateChanged += OnParentWindowStateChanged;
_currentWindow.ContentRendered += OnWindowContentRendered;
SubscribeToSystemParameters();
}
private void OnUnloaded(object sender, RoutedEventArgs e)
{
Loaded -= OnLoaded;
Unloaded -= OnUnloaded;
Appearance.ApplicationThemeManager.Changed -= OnThemeChanged;View on GitHub (pinned to ffebacd610)
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.
Example fix
// before var bar = new TitleBar(); somePanel.Children.Add(bar); // panel not in a window yet -> throws on Loaded // after var window = new FluentWindow(); window.Content = bar; // bar is rooted in a window before Loaded fires
Defensive patterns
Strategy: validation
Validate before calling
if (Window.GetWindow(titleBar) is null)
{
throw new InvalidOperationException("TitleBar must be hosted in a Window before use.");
} Type guard
static bool IsInWindow(FrameworkElement e) => Window.GetWindow(e) is not null;
Try / catch
try { titleBar.RaiseEvent(new RoutedEventArgs(FrameworkElement.LoadedEvent)); }
catch (InvalidOperationException ex) when (ex.Message == "Window is null")
{
_logger.LogError(ex, "TitleBar loaded outside a Window");
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Unable to determine the window source.
- IconElement should have only 1 child
- Use {nameof(CreateIconElement)}
- dpiScale is not initialized.
- Template part '{name}' is not found or is not of type {typeo
AI-assisted analysis of lepoco/wpfui@ffebacd610 (2026-08-13).
Data as JSON: /api/errors/d67e09eb4093b4cc.
Report an issue: GitHub.