{"record":{"id":"e4de1a11ad95ec28","repo":"MahApps/MahApps.Metro","slug":"the-context-context-is-not-inside-a-metrowindo","errorCode":null,"errorMessage":"The context `{context}` is not inside a MetroWindow.","messagePattern":"The context `(.+?)` is not inside a MetroWindow\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/MahApps.Metro/Controls/Dialogs/DialogCoordinator.cs","lineNumber":95,"sourceCode":"        }\n\n        private static MetroWindow GetMetroWindow(object context)\n        {\n            if (context is null)\n            {\n                throw new ArgumentNullException(nameof(context));\n            }\n\n            if (DialogParticipation.IsRegistered(context) == false)\n            {\n                throw new InvalidOperationException($\"The context `{context}` is not registered. Consider using the DialogParticipation.Register property in XAML to bind in the DataContext.\");\n            }\n\n            var association = DialogParticipation.GetAssociation(context);\n            var metroWindow = association.Invoke(() => Window.GetWindow(association) as MetroWindow);\n            if (metroWindow is null)\n            {\n                throw new InvalidOperationException($\"The context `{context}` is not inside a MetroWindow.\");\n            }\n\n            return metroWindow;\n        }\n    }\n}","sourceCodeStart":77,"sourceCodeEnd":101,"githubUrl":"https://github.com/MahApps/MahApps.Metro/blob/72099e310bac2d12ac98fd7560b69679252519f5/src/MahApps.Metro/Controls/Dialogs/DialogCoordinator.cs#L77-L101","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Host the registered view inside a MetroWindow (not a plain Window) so Window.GetWindow resolves to a MetroWindow.","Move DialogParticipation.Register onto the MetroWindow itself (bound to its DataContext) rather than a nested UserControl.","Only call DialogCoordinator methods after the view has loaded and is in the visual tree (e.g. in a Loaded handler, not the constructor).","If you must use a non-Metro window, migrate it to MetroWindow or show dialogs through a different host."],"exampleFix":"<!-- before: plain Window hosts the view -->\n<Window ...\n        xmlns:dialog=\"clr-namespace:MahApps.Metro.Controls.Dialogs\"\n        dialog:DialogParticipation.Register=\"{Binding}\">\n  ... \n</Window>\n<!-- after: use MetroWindow -->\n<controls:MetroWindow ...\n        xmlns:controls=\"http://metro.mahapps.com/winfx/xaml/controls\"\n        xmlns:dialog=\"clr-namespace:MahApps.Metro.Controls.Dialogs\"\n        dialog:DialogParticipation.Register=\"{Binding}\">\n  ...\n</controls:MetroWindow>","handlingStrategy":"validation","validationCode":"// Verify the registered element lives inside a MetroWindow before calling\nvar assoc = DialogParticipation.GetAssociation(context);\nvar owner = assoc.Invoke(() => Window.GetWindow(assoc));\nif (owner is not MetroWindow) {\n    throw new InvalidOperationException(\n        $\"Context '{context}' is registered on {assoc.GetType().Name} which is not inside a MetroWindow.\");\n}","typeGuard":"static bool IsInsideMetroWindow(DependencyObject element) {\n    var window = Window.GetWindow(element);\n    return window is MetroWindow;\n}","tryCatchPattern":"try {\n    await dialogCoordinator.ShowMessageAsync(context, \"Hello\");\n} catch (InvalidOperationException ex) when (ex.Message.Contains(\"not inside a MetroWindow\")) {\n    // migrate host window to MetroWindow, or attach Register to the MetroWindow itself\n}","preventionTips":["Host registered views inside a MetroWindow, not a plain System.Windows.Window.","Attach DialogParticipation.Register to the MetroWindow and bind to its DataContext.","Call the coordinator only after the view is loaded and in the visual tree.","Migrate any non-Metro hosting windows to MetroWindow before enabling dialogs."],"tags":["wpf","dialog","metro-window","mvvm","runtime"],"backgroundTag":null,"analyzedSha":"72099e310bac2d12ac98fd7560b69679252519f5","analyzedAt":"2026-08-13T20:19:34.419Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}