MahApps/MahApps.Metro · error · InvalidOperationException

The context `{context}` is not registered. Consider using th

Error message

The context `{context}` is not registered. Consider using the DialogParticipation.Register property in XAML to bind in the DataContext.

What it means

DialogCoordinator.GetMetroWindow(context) requires the context object to be registered first via the DialogParticipation.Register attached property. It calls DialogParticipation.IsRegistered(context); if false it throws InvalidOperationException telling you to set DialogParticipation.Register in XAML bound to the DataContext. This guards every DialogCoordinator call that needs the hosting window.

Source

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

        }

        public Task<TDialog?> GetCurrentDialogAsync<TDialog>(object context)
            where TDialog : BaseMetroDialog
        {
            var metroWindow = GetMetroWindow(context);
            return metroWindow.Invoke(() => metroWindow.GetCurrentDialogAsync<TDialog>());
        }

        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. Attach DialogParticipation.Register to the root element of the View whose DataContext is the VM, bound to the same context you pass to DialogCoordinator (e.g. DialogParticipation.Register="{Binding}" on the MetroWindow or UserControl).
  2. Ensure the SAME object reference is used for registration and for the DialogCoordinator call (do not pass a freshly constructed copy).
  3. Register before showing the dialog (registration happens in XAML when the view loads); avoid calling the coordinator before the view is loaded.
  4. Set MetroDialogSettings.OwnerCanCloseWithDialog appropriately and confirm the owning view is alive at call time.

Example fix

<!-- before: VM calls ShowMessageAsync(this) but nothing registers it -->
<MetroWindow .../>
// VM:
await _dialogCoord.ShowMessageAsync(this, "Hi");
<!-- after: register the DataContext on the view -->
<MetroWindow ... DialogParticipation.Register="{Binding RelativeSource={RelativeSource Self}, Path=DataContext}">
Defensive patterns

Strategy: validation

Validate before calling

// Before calling the coordinator, verify the context is registered
if (!DialogParticipation.IsRegistered(context)) {
    throw new InvalidOperationException(
        $"Register '{context}' via DialogParticipation.Register on the view before using DialogCoordinator.");
}
await dialogCoordinator.ShowMessageAsync(context, "Hello");

Try / catch

try {
    await dialogCoordinator.ShowMessageAsync(context, "Hello");
} catch (InvalidOperationException ex) when (ex.Message.Contains("not registered")) {
    // register and retry, or surface a setup error to the developer
}

Prevention

When it happens

Trigger: Calling any DialogCoordinator method (ShowMessageAsync, ShowModalAsync, ShowMetroDialogAsync, GetCurrentDialogAsync, etc.) with a context object that was never registered via DialogParticipation.SetRegister / the Register attached property on a view. The internal ContextRegistrationIndex does not contain the object.

Common situations: A ViewModel calls DialogCoordinator instance methods but the corresponding View never sets <i:Interaction.Behaviors> DialogParticipation.Register="{Binding}" (or attaches it to the wrong element). The context passed is a different object than the one registered (new instance, boxed value equality). View unloaded and unregistered the context before the call.

Related errors


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