MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · InvalidOperationException
Cannot cancel dialog closing after {nameof(IsOpen)} property
Error message
Cannot cancel dialog closing after {nameof(IsOpen)} property has been set to {bool.FalseString} What it means
Thrown inside the IsOpen property-changed callback after the host has already set IsOpen=false and run the closing sequence. A DialogSession.Close that cancels closing is invalid at that point: the host cannot re-open a dialog whose IsOpen is already false, so a closing handler calling e.Cancel() (which keeps IsEnded false) triggers the guard at DialogHost.cs:354.
Source
Thrown at src/MaterialDesignThemes.Wpf/DialogHost.cs:354
// * the attached property (which should be applied to the button which opened the dialog
// * straight forward IsOpen dependency property
// * handler provided to the async show method
var dialogClosedEventArgs = new DialogClosedEventArgs(dialogHost.CurrentSession, DialogClosedEvent);
dialogHost.OnDialogClosed(dialogClosedEventArgs);
dialogHost._attachedDialogClosedEventHandler?.Invoke(dialogHost, dialogClosedEventArgs);
dialogHost.DialogClosedCallback?.Invoke(dialogHost, dialogClosedEventArgs);
dialogHost._asyncShowClosedEventHandler?.Invoke(dialogHost, dialogClosedEventArgs);
dialogHost._attachedDialogClosedEventHandler = null;
if (!session.IsEnded)
{
session.Close(session.CloseParameter);
}
//DialogSession.Close may attempt to cancel the closing of the dialog.
//When the dialog is closed in this manner it is not valid
if (!session.IsEnded)
{
throw new InvalidOperationException($"Cannot cancel dialog closing after {nameof(IsOpen)} property has been set to {bool.FalseString}");
}
closeParameter = session.CloseParameter;
dialogHost.CurrentSession = null;
}
//NB: _dialogTaskCompletionSource is only set in the case where the dialog is shown with Show
dialogHost._dialogTaskCompletionSource?.TrySetResult(closeParameter);
// Don't attempt to Invoke if _restoreFocusDialogClose hasn't been assigned yet. Can occur
// if the MainWindow has started up minimized. Even when Show() has been called, this doesn't
// seem to have been set.
dialogHost.Dispatcher.InvokeAsync(() => dialogHost._restoreFocusDialogClose?.Focus(), DispatcherPriority.Input);
return;
}
dialogHost.CurrentSession = new DialogSession(dialogHost);
var window = Window.GetWindow(dialogHost);View on GitHub (pinned to 98edec3a0b)
Solutions
- Do not call DialogSession.Close inside a DialogClosingEventHandler or after IsOpen has been set to false; let the close proceed.
- If you must react, only cancel the close BEFORE IsOpen flips (set e.Cancel in the closing handler) and never call session.Close during closing.
- Audit any code path that sets IsOpen=false directly via binding/SetCurrentValue and remove custom cancel logic there.
- Restructure so cancellation happens in the closing event (DialogClosingEventArgs.Cancel) rather than via session.Close after the fact.
Example fix
// before (throws)
private void Host_DialogClosing(object s, DialogClosingEventArgs e)
{
e.Session.Close(e.Parameter); // session.IsEnded stays false -> throw
}
// after
private void Host_DialogClosing(object s, DialogClosingEventArgs e)
{
if (mustCancel) e.Cancel(); // legitimate cancellation before IsOpen flips
} Defensive patterns
Strategy: validation
Validate before calling
// In a DialogClosing handler, do not call session.Close; only cancel if needed.
private void Host_DialogClosing(object? s, DialogClosingEventArgs e)
{
if (KeepOpenCondition)
e.Cancel(); // valid before IsOpen flips; do NOT call e.Session.Close here
} Prevention
- Never call DialogSession.Close inside DialogClosing/DialogClosed handlers.
- If you must prevent close, set DialogClosingEventArgs.Cancel before IsOpen flips to false.
- Audit any code path that sets IsOpen=false directly and remove cancel-then-close logic.
When it happens
Trigger: A DialogClosingEventHandler calls session.Close(...) or sets IsCancelled during the final closing pass, or programmatic InternalClose is invoked while the property transition is mid-flight and the session tries to cancel. Specifically: session.Close in the closing callback leaves IsEnded false, hitting the check.
Common situations: Custom closing handlers that conditionally cancel; view-model logic that calls session.Close inside DialogClosing; re-entrant close attempts after IsOpen was already set to false programmatically or via binding.
Related errors
- {nameof(DialogHost)} does not have a current session
- Dialog session has ended.
- DialogHost is not open.
- No loaded DialogHost instances.
- DialogHost is already open.
AI-assisted analysis of MaterialDesignInXAML/MaterialDesignInXamlToolkit@98edec3a0b (2026-08-13).
Data as JSON: /api/errors/0badc72ff8014ef2.
Report an issue: GitHub.