MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · ArgumentNullException

childDependencyObject

Error message

childDependencyObject

What it means

ArgumentNullException(nameof(childDependencyObject)) in GetOwningDialogHost(DependencyObject) (DialogHostEx.cs:206). The ShowDialog(DependencyObject, ...) extension methods walk up the visual ancestry from this object; a null DependencyObject cannot start the walk.

Source

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

        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));

        DialogHost? dialogHost = childDependencyObject.GetVisualAncestry().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 in visual tree ancestry with identifier {dialogIdentifier}");

        return dialogHost;

View on GitHub (pinned to 98edec3a0b)

Solutions

  1. Null-check the DependencyObject before calling ShowDialog.
  2. Resolve the control from a non-null source (x:Name field after Loaded, or sender cast with 'is').
  3. Prefer DialogHost.Show(content, identifier) so you do not depend on a child control reference.

Example fix

// before
((Button)sender).ShowDialog(content); // sender could be null

// after
if (sender is DependencyObject d)
    await d.ShowDialog(content);
Defensive patterns

Strategy: validation

Validate before calling

if (childDependencyObject is null)
    throw new ArgumentNullException(nameof(childDependencyObject));

await childDependencyObject.ShowDialog(content);

Type guard

static bool HasOwningDialogHost(DependencyObject? d) =>
    d is not null && d.GetVisualAncestry().OfType<DialogHost>().Any();

Prevention

When it happens

Trigger: Calling someControl.ShowDialog(content) where someControl is null (e.g. a not-yet-initialized control field, a disposed element, or a null from a failed FindName).

Common situations: Showing a dialog from an event whose sender is cast/null; using a control reference before InitializeComponent resolves it; refactor leaving a control field null.

Related errors


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