MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · ArgumentNullException

content

Error message

content

What it means

`DialogHost.Show(...)` requires a non-null `content` argument (the control or view model to display). If null is passed it throws `ArgumentNullException(nameof(content))` before resolving the host instance. The message text is just the parameter name "content".

Source

Thrown at src/MaterialDesignThemes.Wpf/DialogHost.cs:174

    /// <param name="dialogIdentifier"><see cref="Identifier"/> of the instance where the dialog should be shown. Typically this will match an identifier set in XAML. <c>null</c> is allowed.</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>
    /// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
    public static Task<object?> Show(object content, object? dialogIdentifier, DialogOpenedEventHandler? openedEventHandler, DialogClosingEventHandler? closingEventHandler)
        => Show(content, dialogIdentifier, openedEventHandler, closingEventHandler, null);

    /// <summary>
    /// Shows a modal dialog. To use, a <see cref="DialogHost"/> instance must be in a visual tree (typically this may be specified towards the root of a Window's XAML).
    /// </summary>
    /// <param name="content">Content to show (can be a control or view model).</param>
    /// <param name="dialogIdentifier"><see cref="Identifier"/> of the instance where the dialog should be shown. Typically this will match an identifier set in XAML. <c>null</c> is allowed.</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>
    /// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
    public static async Task<object?> Show(object content, object? dialogIdentifier, DialogOpenedEventHandler? openedEventHandler, DialogClosingEventHandler? closingEventHandler, DialogClosedEventHandler? closedEventHandler)
    {
        if (content is null) throw new ArgumentNullException(nameof(content));
        return await GetInstance(dialogIdentifier).ShowInternal(content, openedEventHandler, closingEventHandler, closedEventHandler);
    }

    /// <summary>
    ///  Close a modal dialog.
    /// </summary>
    /// <param name="dialogIdentifier"> of the instance where the dialog should be closed. Typically this will match an identifier set in XAML. </param>
    public static void Close(object? dialogIdentifier)
        => Close(dialogIdentifier, null);

    /// <summary>
    ///  Close a modal dialog.
    /// </summary>
    /// <param name="dialogIdentifier"> of the instance where the dialog should be closed. Typically this will match an identifier set in XAML. </param>
    /// <param name="parameter"> Value returned by DialogHost.ShowDialog(...) or passed to close handler if one is provided.</param>
    public static void Close(object? dialogIdentifier, object? parameter)
    {
        DialogHost dialogHost = GetInstance(dialogIdentifier);

View on GitHub (pinned to 98edec3a0b)

Solutions

  1. Pass a non-null content object (a `UserControl`, `FrameworkElement`, or view model) to `Show`.
  2. Null-check the content before calling: `if (content is null) return;`.
  3. Ensure any DI/resolver that supplies the content returns a real instance.
  4. Construct the dialog view model eagerly when the host is created.

Example fix

// before
await DialogHost.Show(_dialogViewModel, "RootDialog"); // _dialogViewModel is null -> throws

// after
_dialogViewModel ??= new DialogViewModel();
await DialogHost.Show(_dialogViewModel, "RootDialog");
Defensive patterns

Strategy: validation

Validate before calling

object content = BuildDialogContent();
if (content is null) return; // or log
await DialogHost.Show(content, DialogIds.Root);

Type guard

static bool HasContent(object? content) => content is not null;

Prevention

When it happens

Trigger: Calling `await DialogHost.Show(null, identifier, ...)` — e.g. the content expression evaluated to null because the view model was not constructed or the XAML resource was missing.

Common situations: Passing a not-yet-initialized view model field; resolving content from a service/locator that returned null; forgetting to instantiate the dialog view model before showing.

Related errors


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