{"record":{"id":"9a668d3d16aa93df","repo":"lepoco/wpfui","slug":"cannot-change-dialoghost-while-the-dialog-is-being","errorCode":null,"errorMessage":"Cannot change DialogHost while the dialog is being shown.","messagePattern":"Cannot change DialogHost while the dialog is being shown\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Wpf.Ui/Controls/ContentDialog/ContentDialog.cs","lineNumber":633,"sourceCode":"    /// <exception cref=\"InvalidOperationException\">\n    /// Thrown if trying to set DialogHost when DialogHostEx is already set, or if trying to change DialogHost while the dialog is being shown.\n    /// </exception>\n    [Obsolete(\"DialogHost is deprecated. Please use DialogHostEx instead.\")]\n    public ContentPresenter? DialogHost\n    {\n        get => _dialogHost;\n        set\n        {\n            if (_dialogHostEx is not null)\n            {\n                throw new InvalidOperationException(\n                    \"Cannot set DialogHost when DialogHostEx is already set.\"\n                );\n            }\n\n            if (IsShowing)\n            {\n                throw new InvalidOperationException(\n                    \"Cannot change DialogHost while the dialog is being shown.\"\n                );\n            }\n\n            if (ReferenceEquals(_dialogHost, value))\n            {\n                return;\n            }\n\n            if (_dialogHost is not null)\n            {\n                ContentDialogHostBehavior.SetIsEnabled(_dialogHost, false);\n            }\n\n            _dialogHost = value;\n\n            if (_dialogHost is not null)\n            {","sourceCodeStart":615,"sourceCodeEnd":651,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/Controls/ContentDialog/ContentDialog.cs#L615-L651","documentation":"Thrown by the DialogHost setter when attempting to change the host while the dialog is currently being shown (IsShowing is true). Changing the rendering host mid-display would corrupt the visual tree/focus state, so the setter blocks the mutation.","triggerScenarios":"Reassigning ContentDialog.DialogHost (or, symmetrically, DialogHostEx) while ShowAsync is in progress and IsShowing is true. The guard fires before the actual assignment.","commonSituations":"Code that swaps hosts in response to navigation/events that occur during a dialog's display, or reusing a dialog and trying to re-point it before the previous ShowAsync completed/closed.","solutions":["Wait for the dialog to close (await ShowAsync to completion) before changing DialogHost/DialogHostEx.","Create a new ContentDialog instance for a different host rather than mutating the host of one already showing.","Gate the assignment on !dialog.IsShowing before setting."],"exampleFix":"// before\ndialog.DialogHost = newHost; // while ShowAsync is running\n\n// after\nawait dialog.ShowAsync();\ndialog.DialogHost = newHost; // safe, IsShowing is false","handlingStrategy":"validation","validationCode":"if (!dialog.IsShowing) { dialog.DialogHost = newHost; }","typeGuard":"static bool CanChangeHost(ContentDialog d) => !d.IsShowing;","tryCatchPattern":null,"preventionTips":["Await ShowAsync completion before changing the host.","Use a new ContentDialog instance for a different host.","Gate host mutations on !IsShowing."],"tags":["content-dialog","host","showasync","state-mutation"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}