MahApps/MahApps.Metro · error · InvalidOperationException

The context `{context}` is not inside a MetroWindow.

Error message

The context `{context}` is not inside a MetroWindow.

What it means

After confirming the context is registered, GetMetroWindow asks the registered DependencyObject for its hosting window via Window.GetWindow(association) as MetroWindow. If the association is not inside a MetroWindow (e.g. it sits in a plain Window or is detached from the visual tree), the cast yields null and the method throws InvalidOperationException.

Source

Thrown at src/MahApps.Metro/Controls/Dialogs/DialogCoordinator.cs:95

        }

        private static MetroWindow GetMetroWindow(object context)
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (DialogParticipation.IsRegistered(context) == false)
            {
                throw new InvalidOperationException($"The context `{context}` is not registered. Consider using the DialogParticipation.Register property in XAML to bind in the DataContext.");
            }

            var association = DialogParticipation.GetAssociation(context);
            var metroWindow = association.Invoke(() => Window.GetWindow(association) as MetroWindow);
            if (metroWindow is null)
            {
                throw new InvalidOperationException($"The context `{context}` is not inside a MetroWindow.");
            }

            return metroWindow;
        }
    }
}

View on GitHub (pinned to 72099e310b)

Solutions

  1. Host the registered view inside a MetroWindow (not a plain Window) so Window.GetWindow resolves to a MetroWindow.
  2. Move DialogParticipation.Register onto the MetroWindow itself (bound to its DataContext) rather than a nested UserControl.
  3. Only call DialogCoordinator methods after the view has loaded and is in the visual tree (e.g. in a Loaded handler, not the constructor).
  4. If you must use a non-Metro window, migrate it to MetroWindow or show dialogs through a different host.

Example fix

<!-- before: plain Window hosts the view -->
<Window ...
        xmlns:dialog="clr-namespace:MahApps.Metro.Controls.Dialogs"
        dialog:DialogParticipation.Register="{Binding}">
  ... 
</Window>
<!-- after: use MetroWindow -->
<controls:MetroWindow ...
        xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
        xmlns:dialog="clr-namespace:MahApps.Metro.Controls.Dialogs"
        dialog:DialogParticipation.Register="{Binding}">
  ...
</controls:MetroWindow>
Defensive patterns

Strategy: validation

Validate before calling

// Verify the registered element lives inside a MetroWindow before calling
var assoc = DialogParticipation.GetAssociation(context);
var owner = assoc.Invoke(() => Window.GetWindow(assoc));
if (owner is not MetroWindow) {
    throw new InvalidOperationException(
        $"Context '{context}' is registered on {assoc.GetType().Name} which is not inside a MetroWindow.");
}

Type guard

static bool IsInsideMetroWindow(DependencyObject element) {
    var window = Window.GetWindow(element);
    return window is MetroWindow;
}

Try / catch

try {
    await dialogCoordinator.ShowMessageAsync(context, "Hello");
} catch (InvalidOperationException ex) when (ex.Message.Contains("not inside a MetroWindow")) {
    // migrate host window to MetroWindow, or attach Register to the MetroWindow itself
}

Prevention

When it happens

Trigger: DialogParticipation.Register was set on an element whose visual-tree ancestor is not a MetroWindow (a standard System.Windows.Window, a UserControl shown without a MetroWindow host, or an element not yet in the visual tree), so Window.GetWindow(association) as MetroWindow returns null.

Common situations: The view is hosted in a regular Window instead of a MetroWindow. DialogParticipation.Register is attached to a UserControl but the host window is not MetroWindow. The element is registered but not yet added to the visual tree when the dialog call happens (during construction, before Loaded).

Related errors


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