{"record":{"id":"d20a72dffc1680bc","repo":"dotnet/wpf","slug":"sr-cantsetownerafterdialogisshown","errorCode":null,"errorMessage":"SR.CantSetOwnerAfterDialogIsShown","messagePattern":"SR\\.CantSetOwnerAfterDialogIsShown","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs","lineNumber":1233,"sourceCode":"                // is not allowed\n                VerifyApiSupported();\n                VerifyContextAndObjectState();\n                return _ownerWindow;\n            }\n            set\n            {\n                // this call ends up throwing an exception if accessing Owner\n                // is not allowed\n                VerifyApiSupported();\n                VerifyContextAndObjectState();\n                if (value == this)\n                {\n                    throw new ArgumentException(SR.CannotSetOwnerToItself);\n                }\n\n                if (_showingAsDialog)\n                {\n                    throw new InvalidOperationException(SR.CantSetOwnerAfterDialogIsShown);\n                }\n\n                if (value != null && value.IsSourceWindowNull)\n                {\n                    // Try to be specific in the error message.\n                    if (value._disposed)\n                    {\n                        throw new InvalidOperationException(SR.CantSetOwnerToClosedWindow);\n                    }\n                    throw new InvalidOperationException(SR.CantSetOwnerWhosHwndIsNotCreated);\n                }\n\n                if ( _ownerWindow == value )\n                {\n                    return;\n                }\n\n                if (!_disposed)","sourceCodeStart":1215,"sourceCodeEnd":1251,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs#L1215-L1251","documentation":"Window.Owner setter throws InvalidOperationException if the window is currently being shown modally via ShowDialog. The owner of a modal dialog cannot be changed while the dialog is displayed because WPF has already established the Win32 owner relationship and modal state. You must set the owner before calling ShowDialog.","triggerScenarios":"Setting window.Owner (or Owner property) while _showingAsDialog is true, i.e. after ShowDialog() has been called on the window and before it closes.","commonSituations":"Assigning an owner from inside the dialog itself (e.g. in Loaded or a button handler), or a parent window trying to re-parent its modal dialog after launch.","solutions":["Set the Owner property before calling ShowDialog.","If ownership must change at runtime, close the dialog, change Owner, then ShowDialog again.","Use WindowStartupLocation and owner setup in the parent right after constructing the dialog."],"exampleFix":"// before\nvar dlg = new OptionsDialog();\ndlg.ShowDialog();\ndlg.Owner = mainWindow;\n// after\nvar dlg = new OptionsDialog { Owner = mainWindow };\ndlg.ShowDialog();","handlingStrategy":"validation","validationCode":"bool canSetOwner(Window w) => w != null && !w.IsLoaded; // set owner before window is loaded/shown modally","typeGuard":null,"tryCatchPattern":"try { dlg.Owner = owner; dlg.ShowDialog(); }\ncatch (InvalidOperationException) { dlg.ShowDialog(); }","preventionTips":["Always set Owner immediately after constructing the dialog, before ShowDialog.","Never mutate window ownership from inside dialog event handlers.","Centralize dialog launching in a helper that wires ownership first."],"tags":["wpf","window","owner","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"}