{"record":{"id":"4b300e22c30bbfd6","repo":"lepoco/wpfui","slug":"cannot-change-dialoghostex-while-the-dialog-is-bei","errorCode":null,"errorMessage":"Cannot change DialogHostEx while the dialog is being shown.","messagePattern":"Cannot change DialogHostEx while the dialog is being shown\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Wpf.Ui/Controls/ContentDialog/ContentDialog.cs","lineNumber":679,"sourceCode":"    /// </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        }\n    }\n\n    [Obsolete(\"ContentPresenter is deprecated. Please use DialogHost instead.\")]\n    public ContentPresenter? ContentPresenter { get; set; } = default;\n\n    protected TaskCompletionSource<ContentDialogResult>? Tcs { get; set; }\n","sourceCodeStart":661,"sourceCodeEnd":697,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/Controls/ContentDialog/ContentDialog.cs#L661-L697","documentation":"Thrown by the ContentDialog.DialogHostEx setter when you attempt to reassign the host while a previous ShowAsync call is still in flight. The dialog tracks an IsShowing flag (Tcs != null and not completed) and refuses host mutations mid-display to avoid tearing down the visual tree under a live await. It is a state-machine guard, not a data error.","triggerScenarios":"Assigning dialog.DialogHostEx = newHost while the Task returned by a prior dialog.ShowAsync() has not yet completed (i.e. the dialog is still on screen or the TCS is pending). Also fires if a binding pushes a new value to DialogHostEx during display.","commonSituations":"Reusing a single ContentDialog instance across windows and swapping the host in a view-model; data-binding DialogHostEx to a property that changes on navigation; calling ShowAsync without await and immediately reassigning the host.","solutions":["Await the previous ShowAsync() call to completion before assigning DialogHostEx.","Set DialogHostEx once at construction / XAML time and do not rebind it.","If you must swap hosts, create a new ContentDialog instance per host rather than mutating the existing one.","Guard the assignment yourself: if (!dialog.IsShowing) dialog.DialogHostEx = host; — note IsShowing is private, so track the ShowAsync task yourself."],"exampleFix":"// before\n_ = dialog.ShowAsync();\ndialog.DialogHostEx = otherHost; // throws\n\n// after\nawait dialog.ShowAsync();\ndialog.DialogHostEx = otherHost;","handlingStrategy":"validation","validationCode":"// Track the in-flight task yourself (IsShowing is private)\nprivate Task<ContentDialogResult>? _showTask;\n\nif (_showTask is null || _showTask.IsCompleted)\n    dialog.DialogHostEx = newHost;\nelse\n    Debug.WriteLine(\"Cannot change DialogHostEx: dialog is showing.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always await ShowAsync before mutating DialogHost/DialogHostEx.","Do not data-bind DialogHostEx to a frequently-changing view-model property.","Prefer one host per ContentDialog for the dialog's lifetime."],"tags":["wpf","contentdialog","state-violation","ui-thread"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}