{"record":{"id":"2160883f46f193ae","repo":"lepoco/wpfui","slug":"cannot-set-dialoghostex-when-dialoghost-is-already","errorCode":null,"errorMessage":"Cannot set DialogHostEx when DialogHost is already set.","messagePattern":"Cannot set DialogHostEx when DialogHost is already set\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Wpf.Ui/Controls/ContentDialog/ContentDialog.cs","lineNumber":672,"sourceCode":"\n            UpdateIsLegacyHost();\n        }\n    }\n\n    /// <summary>\n    /// Gets or sets <see cref=\"DialogHostEx\"/> inside of which the dialogue will be placed.\n    /// </summary>\n    /// <exception cref=\"InvalidOperationException\">\n    /// Thrown if trying to set DialogHostEx when DialogHost is already set, or if trying to change DialogHostEx while the dialog is being shown.\n    /// </exception>\n    public ContentDialogHost? DialogHostEx\n    {\n        get => _dialogHostEx;\n        set\n        {\n            if (_dialogHost is not null)\n            {\n                throw new InvalidOperationException(\n                    \"Cannot set DialogHostEx when DialogHost is already set.\"\n                );\n            }\n\n            if (IsShowing)\n            {\n                throw new InvalidOperationException(\n                    \"Cannot change DialogHostEx while the dialog is being shown.\"\n                );\n            }\n\n            if (!ReferenceEquals(_dialogHostEx, value))\n            {\n                _dialogHostEx = value;\n            }\n\n            UpdateIsLegacyHost();\n        }","sourceCodeStart":654,"sourceCodeEnd":690,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/Controls/ContentDialog/ContentDialog.cs#L654-L690","documentation":"Thrown by the DialogHostEx setter when the legacy DialogHost is already set on the same ContentDialog. It is the symmetric guard to error 17: once a ContentPresenter host is assigned, setting the new ContentDialogHost on that dialog is rejected to avoid mixed hosting.","triggerScenarios":"Assigning ContentDialog.DialogHostEx = someHost after ContentDialog.DialogHost was already assigned (or bound in XAML). The _dialogHost field is non-null at set time, triggering the throw.","commonSituations":"XAML that still binds the obsolete DialogHost property plus code that sets DialogHostEx, or migration where the old binding was not removed before introducing the new host.","solutions":["Remove the legacy DialogHost assignment/binding and use DialogHostEx exclusively.","Audit XAML for DialogHost= bindings (CS0618) and convert them to DialogHostEx.","Ensure a single host assignment strategy per dialog across both XAML and code-behind."],"exampleFix":"// before\n<ui:ContentDialog DialogHost=\"{Binding ElementName=MyPresenter}\" />\n... \ndialog.DialogHostEx = myHost; // throws, DialogHost already set\n\n// after\n<ui:ContentDialog DialogHostEx=\"{Binding ElementName=MyDialogHost}\" />","handlingStrategy":"validation","validationCode":"if (dialog.DialogHost is null) { dialog.DialogHostEx = host; }","typeGuard":"static bool CanSetHostEx(ContentDialog d) => d.DialogHost is null && !d.IsShowing;","tryCatchPattern":null,"preventionTips":["Remove legacy DialogHost bindings before using DialogHostEx.","Audit XAML for DialogHost= and migrate to DialogHostEx.","Maintain a single host assignment strategy per dialog."],"tags":["content-dialog","host","api-migration","obsolete"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}