MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · InvalidOperationException

Unable to find a DialogHost with identifier '{dialogIdentifi

Error message

Unable to find a DialogHost with identifier '{dialogIdentifier}' in visual tree

What it means

InvalidOperationException('Unable to find a DialogHost with identifier ... in visual tree') from GetFirstDialogHost(Window, identifier) (DialogHostEx.cs:199). A DialogHost exists in the Window's visual tree but none has an Identifier equal to the supplied dialogIdentifier.

Source

Thrown at src/MaterialDesignThemes.Wpf/DialogHostEx.cs:199

    {
        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)
    {
        if (childDependencyObject is null) throw new ArgumentNullException(nameof(childDependencyObject));

        DialogHost? dialogHost = childDependencyObject.GetVisualAncestry().OfType<DialogHost>().FirstOrDefault();

        if (dialogHost is null)
            throw new InvalidOperationException("Unable to find a DialogHost in visual tree ancestry");

        return dialogHost;
    }
    private static DialogHost GetOwningDialogHost(DependencyObject childDependencyObject, object dialogIdentifier)
    {
        if (childDependencyObject is null) throw new ArgumentNullException(nameof(childDependencyObject));

View on GitHub (pinned to 98edec3a0b)

Solutions

  1. Set 'Identifier="MyId"' on the DialogHost in XAML matching exactly the code-side identifier.
  2. Verify identifier type parity (string vs object/enum); Equality is used so types must match.
  3. Ensure the target DialogHost is in the same Window whose ShowDialog you call, or use DialogHost.Show(content, identifier) to search all loaded hosts.

Example fix

<!-- before -->
<materialDesign:DialogHost> ... </materialDesign:DialogHost>

// code: await window.ShowDialog(content, "RootDialog"); -> mismatch

<!-- after -->
<materialDesign:DialogHost Identifier="RootDialog"> ... </materialDesign:DialogHost>
Defensive patterns

Strategy: validation

Validate before calling

bool hasMatching = window.VisualDepthFirstTraversal()
    .OfType<DialogHost>()
    .Any(h => h.Identifier is not null && h.Identifier.Equals(identifier));
if (!hasMatching)
    throw new InvalidOperationException($"No DialogHost with Identifier '{identifier}' in this Window.");

await window.ShowDialog(content, identifier);

Type guard

static bool WindowHasHostWithIdentifier(Window window, object? identifier) =>
    window.VisualDepthFirstTraversal().OfType<DialogHost>()
        .Any(h => h.Identifier is not null && h.Identifier.Equals(identifier));

Try / catch

try
{
    await window.ShowDialog(content, identifier);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("with identifier"))
{
    // fix XAML Identifier or switch to DialogHost.Show
}

Prevention

When it happens

Trigger: Calling window.ShowDialog(content, "MyId") where no DialogHost in that Window has Identifier="MyId" (typo, missing Identifier, wrong string, or a type mismatch like string vs enum).

Common situations: Identifier typo between XAML and code; Identifier not set in XAML; string vs enum/object identifier mismatch (e.g. passing 'MyId' string when XAML used an enum); DialogHost is in another Window.

Related errors


AI-assisted analysis of MaterialDesignInXAML/MaterialDesignInXamlToolkit@98edec3a0b (2026-08-13). Data as JSON: /api/errors/fc667ad1050cb5d3. Report an issue: GitHub.