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
- 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).
- Ensure the SAME object reference is used for registration and for the DialogCoordinator call (do not pass a freshly constructed copy).
- Register before showing the dialog (registration happens in XAML when the view loads); avoid calling the coordinator before the view is loaded.
- 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
- Set DialogParticipation.Register="{Binding}" on the MetroWindow (or the host element) whose DataContext is the VM.
- Use the same object reference for registration and for DialogCoordinator calls.
- Call DialogCoordinator methods only after the view has Loaded (registration happens on load).
- Add a startup/design-time check that asserts the VM is registered.
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
- The context `{context}` is not inside a MetroWindow.
- The inverse dialog theme only works if the window theme abid
- Unable to find the dialog closing storyboard. Did you forget
- Active dialog container could not be found.
- Inactive dialog container could not be found.
AI-assisted analysis of MahApps/MahApps.Metro@72099e310b (2026-08-13).
Data as JSON: /api/errors/bed83d5570e2bf60.
Report an issue: GitHub.