MahApps/MahApps.Metro · error · InvalidOperationException

Active dialog container could not be found.

Error message

Active dialog container could not be found.

What it means

DialogManager.HandleOverlayOnHideAsync runs after a dialog closes to manage the overlay and close button state. It requires window.metroActiveDialogContainer (a Grid resolved from the MetroWindow control template part PART_MetroActiveDialogContainer). If that field is null the method throws InvalidOperationException because there is no container to inspect/clear. The same message is reused at multiple call sites.

Source

Thrown at src/MahApps.Metro/Controls/Dialogs/DialogManager.cs:220

                DialogClosed?.Invoke(window, new DialogStateChangedEventArgs(dialog));

                await dialog.WaitForCloseAsync();

                window.SizeChanged -= dialog.SizeChangedHandler;
                window.RemoveDialog(dialog);

                await HandleOverlayOnHideAsync(settings, window);
            }

            return new ProgressDialogController(dialog, CloseCallBack);
        }

        private static async Task HandleOverlayOnHideAsync(MetroDialogSettings? settings, MetroWindow window)
        {
            if (window.metroActiveDialogContainer is null)
            {
                throw new InvalidOperationException("Active dialog container could not be found.");
            }

            var isAnyDialogOpen = window.metroActiveDialogContainer.Children.OfType<BaseMetroDialog>().Any();
            if (!isAnyDialogOpen)
            {
                if (settings is null || settings.AnimateHide)
                {
                    await window.HideOverlayAsync();
                }
                else
                {
                    // ReSharper disable once MethodHasAsyncOverload
                    window.HideOverlay();
                }
            }

            if (window.metroActiveDialogContainer.Children.Count == 0)
            {

View on GitHub (pinned to 72099e310b)

Solutions

  1. Restore the PART_MetroActiveDialogContainer (and PART_MetroInactiveDialogsContainer) Grid(s) in your custom MetroWindow ControlTemplate so GetTemplateChild resolves them.

Example fix

<!-- before: custom MetroWindow template missing the dialog part -->
<ControlTemplate TargetType="{x:Type controls:MetroWindow}">
  <Grid> ... <!-- no active dialog container --> </Grid>
</ControlTemplate>
<!-- after: include the required named part -->
<ControlTemplate TargetType="{x:Type controls:MetroWindow}">
  <Grid>
    ...
    <Grid x:Name="PART_MetroActiveDialogContainer"/>
    <Grid x:Name="PART_MetroInactiveDialogsContainer"/>
  </Grid>
</ControlTemplate>
Defensive patterns

Strategy: validation

Validate before calling

// Before showing/hiding dialogs, assert the template part resolved
if (window.metroActiveDialogContainer is null) {
    throw new InvalidOperationException(
        "MetroWindow template is missing PART_MetroActiveDialogContainer; cannot manage dialogs.");
}

Try / catch

try {
    await dialogCoordinator.HideMetroDialogAsync(context, dialog);
} catch (InvalidOperationException ex) when (ex.Message.Contains("Active dialog container could not be found")) {
    // alert that the MetroWindow template is missing the required part; do not retry
}

Prevention

When it happens

Trigger: A dialog is being hidden/removed from a MetroWindow whose template never supplied the PART_MetroActiveDialogContainer part — i.e. a heavily customized MetroWindow ControlTemplate that omitted the required template part — so the field resolved in OnApplyTemplate/MetroWindow.cs:1426 stayed null.

Common situations: A custom MetroWindow ControlTemplate was authored without the 'PART_MetroActiveDialogContainer' Grid (and the inactive container). Someone replaced the default MetroWindow template for styling and dropped the required parts. Wrong/older template resource merged that predates the dialog feature.

Related errors


AI-assisted analysis of MahApps/MahApps.Metro@72099e310b (2026-08-13). Data as JSON: /api/errors/6a097fa36103f800. Report an issue: GitHub.