MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · InvalidOperationException
Unable to find a DialogHost in visual tree
Error message
Unable to find a DialogHost in visual tree
What it means
InvalidOperationException('Unable to find a DialogHost in visual tree') from GetFirstDialogHost(Window) (DialogHostEx.cs:187). A depth-first visual traversal of the Window found zero DialogHost elements, so ShowDialog cannot proceed.
Source
Thrown at src/MaterialDesignThemes.Wpf/DialogHostEx.cs:187
/// <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");
return dialogHost;
}
private static DialogHost GetOwningDialogHost(DependencyObject childDependencyObject)
{View on GitHub (pinned to 98edec3a0b)
Solutions
- Add a <materialDesign:DialogHost> to the Window's XAML (typically wrapping the root content).
- Call ShowDialog on the correct Window that actually contains the DialogHost, or pass the host's Identifier and use DialogHost.Show(content, identifier).
- Ensure the call happens after the Window's visual tree is constructed (Loaded event).
Example fix
<!-- before: Window has no DialogHost -->
<Window ...>
<Grid> ... </Grid>
</Window>
<!-- after -->
<Window ...>
<materialDesign:DialogHost Identifier="RootDialog">
<Grid> ... </Grid>
</materialDesign:DialogHost>
</Window> Defensive patterns
Strategy: validation
Validate before calling
bool hasHost = window.VisualDepthFirstTraversal().OfType<DialogHost>().Any();
if (!hasHost)
throw new InvalidOperationException("Add a DialogHost to this Window before calling ShowDialog.");
await window.ShowDialog(content); Type guard
static bool WindowHasDialogHost(Window window) => window.VisualDepthFirstTraversal().OfType<DialogHost>().Any();
Try / catch
try
{
await window.ShowDialog(content);
}
catch (InvalidOperationException ex) when (ex.Message == "Unable to find a DialogHost in visual tree")
{
// add a DialogHost or call DialogHost.Show(content, identifier)
} Prevention
- Add a DialogHost to the Window's XAML before using Window.ShowDialog.
- Call ShowDialog after the Window's Loaded event so the visual tree is built.
- If multiple Windows exist, address the host by Identifier with DialogHost.Show.
When it happens
Trigger: Calling window.ShowDialog(content) on a Window whose XAML/template contains no <materialDesign:DialogHost>, or the DialogHost is in a different Window/popup not rooted under this Window.
Common situations: Forgetting to add a DialogHost to the Window; DialogHost defined in a different Window than the one passed; calling before the visual tree is built (host in a lazy-loaded template).
Related errors
- Unable to find a DialogHost with identifier '{dialogIdentifi
- Unable to find a DialogHost in visual tree ancestry
- Unable to find a DialogHost in visual tree ancestry with ide
- Content cannot be passed to a dialog via the OpenDialog if D
- childDependencyObject
AI-assisted analysis of MaterialDesignInXAML/MaterialDesignInXamlToolkit@98edec3a0b (2026-08-13).
Data as JSON: /api/errors/0b1fecddcee3aff2.
Report an issue: GitHub.