lepoco/wpfui · error · InvalidOperationException
Cannot set DialogHost when DialogHostEx is already set.
Error message
Cannot set DialogHost when DialogHostEx is already set.
What it means
Thrown by the DialogHost property setter (deprecated) when the new-style DialogHostEx is already set on the same ContentDialog. The control forbids assigning both host types to prevent ambiguous rendering, so setting the legacy host after DialogHostEx is rejected.
Source
Thrown at src/Wpf.Ui/Controls/ContentDialog/ContentDialog.cs:626
// Legacy and new host coexist for compatibility during migration.
private ContentPresenter? _dialogHost;
private ContentDialogHost? _dialogHostEx;
/// <summary>
/// Gets or sets <see cref="DialogHost"/> inside of which the dialogue will be placed.
/// </summary>
/// <exception cref="InvalidOperationException">
/// Thrown if trying to set DialogHost when DialogHostEx is already set, or if trying to change DialogHost while the dialog is being shown.
/// </exception>
[Obsolete("DialogHost is deprecated. Please use DialogHostEx instead.")]
public ContentPresenter? DialogHost
{
get => _dialogHost;
set
{
if (_dialogHostEx is not null)
{
throw new InvalidOperationException(
"Cannot set DialogHost when DialogHostEx is already set."
);
}
if (IsShowing)
{
throw new InvalidOperationException(
"Cannot change DialogHost while the dialog is being shown."
);
}
if (ReferenceEquals(_dialogHost, value))
{
return;
}
if (_dialogHost is not null)
{View on GitHub (pinned to ffebacd610)
Solutions
- Standardize on DialogHostEx and remove all assignments to the deprecated DialogHost property.
- Search for DialogHost = (CS0618 obsolete) usages and migrate them to DialogHostEx.
- Ensure only one host assignment path runs per dialog instance.
Example fix
// before dialog.DialogHost = myContentPresenter; // DialogHostEx already set // after dialog.DialogHostEx = myContentDialogHost;
Defensive patterns
Strategy: validation
Validate before calling
if (dialog.DialogHostEx is null) { dialog.DialogHost = presenter; } Type guard
static bool CanSetLegacyHost(ContentDialog d) => d.DialogHostEx is null && !d.IsShowing;
Prevention
- Use DialogHostEx exclusively; remove deprecated DialogHost assignments.
- Resolve CS0618 warnings to surface legacy host code.
- Keep one host assignment path per dialog.
When it happens
Trigger: Assigning ContentDialog.DialogHost = someContentPresenter after ContentDialog.DialogHostEx has already been assigned (or vice-versa guard at error 19). The _dialogHostEx field is non-null at set time.
Common situations: Migrating from the legacy ContentPresenter host to ContentDialogHost but leaving XAML/code that still assigns DialogHost; or shared dialog instances receiving both host assignments from different code paths.
Related errors
- Cannot set ContentPresenter: a ContentDialogHost host has al
- Cannot set ContentDialogHost: a legacy ContentPresenter host
- Cannot set DialogHostEx when DialogHost is already set.
- The DialogHost was never set.
- The DialogHost is not the same as the one that was previousl
AI-assisted analysis of lepoco/wpfui@ffebacd610 (2026-08-13).
Data as JSON: /api/errors/6a62b574e3230d72.
Report an issue: GitHub.