dotnet/wpf · error · InvalidOperationException
SR.DialogResultMustBeSetAfterShowDialog
Error message
SR.DialogResultMustBeSetAfterShowDialog
What it means
The DialogResult property setter throws InvalidOperationException if the window was not shown with ShowDialog. DialogResult only has meaning for modal dialogs; setting it on a non-modal window (shown with Show) is invalid because there is no ShowDialog call to return to.
Solutions
- Ensure the window is opened with ShowDialog() when DialogResult is set.
- Instead of DialogResult, use Close() and a custom property/event for non-modal windows.
- Guard the setter with a check of your own modal flag before assigning DialogResult.
Example fix
// before resultWindow.Show(); resultWindow.DialogResult = true; // throws // after resultWindow.ShowDialog(); // DialogResult now allowed
Defensive patterns
Strategy: validation
Validate before calling
bool canSetDialogResult(Window w, bool shownModally) => shownModally; // track whether window was opened via ShowDialog
Try / catch
try { w.DialogResult = true; }
catch (InvalidOperationException) { w.Close(); } Prevention
- Ensure the window is always opened with ShowDialog when its code sets DialogResult.
- Use a dedicated result property plus Close() for non-modal windows.
- Document the modal contract on the window class.
When it happens
Trigger: Setting this.DialogResult = true/false in code-behind of a window that was displayed via Show() instead of ShowDialog().
Common situations: Sharing a window class sometimes opened modally and sometimes not; setting DialogResult in a button handler whose window was activated non-modally; code converted from MessageBox-style flows.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- SR.CantSetOwnerAfterDialogIsShown
- SR.InvalidCompositionTarget
- SR.InvalidOperationDuringClosing
- SR.NotAllowedBeforeShow
- SR.ShowNonActivatedAndMaximized
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/ce9f67b767aa5191.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs:1382
{
_dialogResult = value;
// if DialogResult is set from within a Closing event then
// the window is in the closing state. Thus, if we call
// Close() again from here we go into an infinite loop.
//
// Note: Windows OS bug # 934500 Setting DialogResult
// on the Closing EventHandler of a Dialog causes StackOverFlowException
if(!_isClosing)
{
Close();
}
}
}
else
{
throw new InvalidOperationException(SR.DialogResultMustBeSetAfterShowDialog);
}
}
}
/// <summary>
/// The DependencyProperty for WindowStyleProperty.
/// Flags: None
/// Default Value: WindowStyle.SingleBorderWindow
/// </summary>
public static readonly DependencyProperty WindowStyleProperty =
DependencyProperty.Register("WindowStyle", typeof(WindowStyle), typeof(Window),
new FrameworkPropertyMetadata(
WindowStyle.SingleBorderWindow,
new PropertyChangedCallback(_OnWindowStyleChanged),
new CoerceValueCallback(CoerceWindowStyle)),
new ValidateValueCallback(_ValidateWindowStyleCallback));
/// <summary>View on GitHub (pinned to 81131a70a4)