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

  1. Standardize on DialogHostEx and remove all assignments to the deprecated DialogHost property.
  2. Search for DialogHost = (CS0618 obsolete) usages and migrate them to DialogHostEx.
  3. 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

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


AI-assisted analysis of lepoco/wpfui@ffebacd610 (2026-08-13). Data as JSON: /api/errors/6a62b574e3230d72. Report an issue: GitHub.