{"record":{"id":"ce9f67b767aa5191","repo":"dotnet/wpf","slug":"sr-dialogresultmustbesetaftershowdialog","errorCode":null,"errorMessage":"SR.DialogResultMustBeSetAfterShowDialog","messagePattern":"SR\\.DialogResultMustBeSetAfterShowDialog","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs","lineNumber":1382,"sourceCode":"                    {\n                        _dialogResult = value;\n\n                        // if DialogResult is set from within a Closing event then\n                        // the window is in the closing state.  Thus, if we call\n                        // Close() again from here we go into an infinite loop.\n                        //\n                        // Note: Windows OS bug # 934500 Setting DialogResult\n                        // on the Closing EventHandler of a Dialog causes StackOverFlowException\n\n                        if(!_isClosing)\n                        {\n                            Close();\n                        }\n                    }\n                }\n                else\n                {\n                    throw new InvalidOperationException(SR.DialogResultMustBeSetAfterShowDialog);\n}\n            }\n        }\n\n        /// <summary>\n        ///     The DependencyProperty for WindowStyleProperty.\n        ///     Flags:              None\n        ///     Default Value:      WindowStyle.SingleBorderWindow\n        /// </summary>\n        public static readonly DependencyProperty WindowStyleProperty =\n                DependencyProperty.Register(\"WindowStyle\", typeof(WindowStyle), typeof(Window),\n                        new FrameworkPropertyMetadata(\n                                WindowStyle.SingleBorderWindow,\n                                new PropertyChangedCallback(_OnWindowStyleChanged),\n                                new CoerceValueCallback(CoerceWindowStyle)),\n                        new ValidateValueCallback(_ValidateWindowStyleCallback));\n\n        /// <summary>","sourceCodeStart":1364,"sourceCodeEnd":1400,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs#L1364-L1400","documentation":"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.","triggerScenarios":"Setting this.DialogResult = true/false in code-behind of a window that was displayed via Show() instead of ShowDialog().","commonSituations":"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.","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."],"exampleFix":"// before\nresultWindow.Show();\nresultWindow.DialogResult = true; // throws\n// after\nresultWindow.ShowDialog(); // DialogResult now allowed","handlingStrategy":"validation","validationCode":"bool canSetDialogResult(Window w, bool shownModally) => shownModally; // track whether window was opened via ShowDialog","typeGuard":null,"tryCatchPattern":"try { w.DialogResult = true; }\ncatch (InvalidOperationException) { w.Close(); }","preventionTips":["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."],"tags":["wpf","window","dialogresult","invalid-operation"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}