MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · ArgumentNullException
window
Error message
window
What it means
ArgumentNullException(nameof(window)) in GetFirstDialogHost(Window) (DialogHostEx.cs:182). Triggered when a ShowDialog extension method on Window is called with a null Window reference, before any visual-tree traversal runs.
Source
Thrown at src/MaterialDesignThemes.Wpf/DialogHostEx.cs:182
/// <summary>
/// Shows a dialog using the parent/ancestor <see cref="DialogHost"/> of the a given <see cref="DependencyObject"/>.
/// </summary>
/// <param name="childDependencyObject">Dependency object which should be a visual child of a <see cref="DialogHost"/>, where the dialog will be shown.</param>
/// <param name="content">Content to show (can be a control or view model).</param>
/// <param name="openedEventHandler">Allows access to opened event which would otherwise have been subscribed to on a instance.</param>
/// <param name="closingEventHandler">Allows access to closing event which would otherwise have been subscribed to on a instance.</param>
/// <param name="closedEventHandler">Allows access to closed event which would otherwise have been subscribed to on a instance.</param>
/// <exception cref="InvalidOperationException">
/// Thrown is a <see cref="DialogHost"/> is not found when conducting a depth first traversal of visual tree.
/// </exception>
/// <returns></returns>
public static Task<object?> ShowDialog(this DependencyObject childDependencyObject, object content, DialogOpenedEventHandler openedEventHandler, DialogClosingEventHandler closingEventHandler, DialogClosedEventHandler closedEventHandler)
=> GetOwningDialogHost(childDependencyObject).ShowInternal(content, openedEventHandler, closingEventHandler, closedEventHandler);
private static DialogHost GetFirstDialogHost(Window window)
{
if (window is null) throw new ArgumentNullException(nameof(window));
DialogHost? dialogHost = window.VisualDepthFirstTraversal().OfType<DialogHost>().FirstOrDefault();
if (dialogHost is null)
throw new InvalidOperationException("Unable to find a DialogHost in visual tree");
return dialogHost;
}
private static DialogHost GetFirstDialogHost(Window window, object? dialogIdentifier)
{
if (window is null) throw new ArgumentNullException(nameof(window));
DialogHost? dialogHost = window.VisualDepthFirstTraversal().OfType<DialogHost>().FirstOrDefault(x => x.Identifier is not null && x.Identifier.Equals(dialogIdentifier));
if (dialogHost is null)
throw new InvalidOperationException($"Unable to find a DialogHost with identifier '{dialogIdentifier}' in visual tree");
View on GitHub (pinned to 98edec3a0b)
Solutions
- Null-check the Window before calling ShowDialog and surface a clearer error or skip.
- Acquire the window via Window.GetWindow(this) only after the control is loaded (subscribe to Loaded).
- Prefer DialogHost.Show(content, identifier) which routes via Identifier and avoids needing a Window reference.
Example fix
// before
Application.Current.MainWindow.ShowDialog(content); // MainWindow may be null at startup
// after
var window = Window.GetWindow(this);
if (window is not null)
await window.ShowDialog(content);
// or: await DialogHost.Show(content, "RootDialog"); Defensive patterns
Strategy: validation
Validate before calling
if (window is null)
throw new ArgumentNullException(nameof(window));
await window.ShowDialog(content); Type guard
static bool CanShow(Window? window) => window is not null && window.VisualDepthFirstTraversal().OfType<DialogHost>().Any();
Prevention
- Resolve the Window via Window.GetWindow(this) inside a Loaded handler, not at construction time.
- Prefer DialogHost.Show(content, identifier) over Window.ShowDialog to avoid null-Window issues.
- Null-check Application.Current.MainWindow before using it.
When it happens
Trigger: Calling window.ShowDialog(content) where 'window' is null (e.g. Window.GetWindow(this) returned null because the control is not yet in a loaded Window, or a field was never assigned).
Common situations: Calling ShowDialog from a control before it is loaded into a Window; using Application.Current.MainWindow before it is set; refactor leaving a Window field null.
Related errors
AI-assisted analysis of MaterialDesignInXAML/MaterialDesignInXamlToolkit@98edec3a0b (2026-08-13).
Data as JSON: /api/errors/d23290ec408b853c.
Report an issue: GitHub.