{"record":{"id":"227ae3fd6d172f65","repo":"lepoco/wpfui","slug":"only-one-contentdialoghost-instance-is-allowed-per","errorCode":null,"errorMessage":"Only one ContentDialogHost instance is allowed per Window.","messagePattern":"Only one ContentDialogHost instance is allowed per Window\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Wpf.Ui/Controls/ContentDialog/ContentDialogHost.cs","lineNumber":220,"sourceCode":"\n    private void RegisterHostForWindow()\n    {\n        var window = Window.GetWindow(this);\n        if (window != null)\n        {\n            RegisterHost(window);\n        }\n    }\n\n    private void RegisterHost(Window window)\n    {\n        lock (WindowHostsLock)\n        {\n            if (WindowHosts.TryGetValue(window, out var existing))\n            {\n                if (!ReferenceEquals(existing, this))\n                {\n                    throw new InvalidOperationException(\n                        \"Only one ContentDialogHost instance is allowed per Window.\"\n                    );\n                }\n\n                // already registered for this window and it's this instance\n                return;\n            }\n\n            WindowHosts.Add(window, this);\n        }\n    }\n}\n\n#pragma warning restore IDE0008 // Use explicit type instead of 'var'\n","sourceCodeStart":202,"sourceCodeEnd":235,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/Controls/ContentDialog/ContentDialogHost.cs#L202-L235","documentation":"Thrown by ContentDialogHost.RegisterHost when a second, distinct ContentDialogHost instance tries to register itself for the same Window. The library keeps a single-slot-per-window map (WindowHosts) because only one host can own the overlay layer for a window; registering a different instance is treated as a programming error rather than silently replacing the first.","triggerScenarios":"Placing two <ui:ContentDialogHost> elements in the same Window's visual tree; adding a second host dynamically after one is already loaded; re-hosting on a window that still holds a previously-loaded host that was never unloaded.","commonSituations":"Migrating an app to WPF UI and leaving an old ContentDialogHost in XAML while adding another in a user control that lives in the same window; merging two user controls each of which ships its own host into one window; a host whose Unloaded handler did not fire (e.g. window closed via ShutdownMode) leaving a stale registration.","solutions":["Keep exactly one ContentDialogHost per Window — typically at the root of the window's content.","If you need hosts in multiple user controls, remove the duplicates and let them share the window-level host.","Ensure the previous host is properly unloaded (removed from the visual tree) before adding a new one.","Check WindowHosts registrations during debugging if you suspect a stale entry."],"exampleFix":"<!-- before -->\n<Window>\n  <StackPanel>\n    <ui:ContentDialogHost x:Name=\"host1\"/>\n    <ui:ContentDialogHost x:Name=\"host2\"/> <!-- throws on load -->\n  </StackPanel>\n</Window>\n\n<!-- after -->\n<Window>\n  <ui:ContentDialogHost x:Name=\"host\"/>\n</Window>","handlingStrategy":"validation","validationCode":"// In a window, ensure only one host exists\nvar hosts = FindVisualChildren<ContentDialogHost>(this).ToList();\nDebug.Assert(hosts.Count <= 1, $\"{hosts.Count} ContentDialogHosts in window\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Place a single ContentDialogHost at the window content root.","Do not ship ContentDialogHost inside reusable user controls that get composed into the same window.","Audit XAML for duplicate hosts when merging user controls."],"tags":["wpf","contentdialog","configuration","singleton"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}