{"record":{"id":"c3818bfdc13391ad","repo":"dotnet/wpf","slug":"sr-cannotsetownertoitself","errorCode":null,"errorMessage":"SR.CannotSetOwnerToItself","messagePattern":"SR\\.CannotSetOwnerToItself","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs","lineNumber":1228,"sourceCode":"        public Window Owner\n        {\n            get\n            {\n                // this call ends up throwing an exception if accessing Owner\n                // 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 )","sourceCodeStart":1210,"sourceCodeEnd":1246,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs#L1210-L1246","documentation":"Setting Window.Owner rejects assigning the window to itself: the setter throws ArgumentException(SR.CannotSetOwnerToItself) when value == this. A window cannot own itself; ownership must form an acyclic parent-child relationship between distinct windows.","triggerScenarios":"window.Owner = window in code; data-binding Owner to a view model property that resolves back to the same window; generic helper code passing the active window as owner without checking identity.","commonSituations":"Helper methods like ShowDialog(Window owner) called with the dialog itself; reflection/dynamic code resolving 'current window' to the target; copy-pasted owner assignment in window initialization.","solutions":["Guard with if (value != this) before assigning Owner","Pass the correct owner window (e.g. Application.Current.MainWindow or the calling window), not the dialog itself","Change helper signatures to never use the target window as its own owner","Use WindowInteropHelper.SetOwner with a verified distinct parent handle when doing interop ownership"],"exampleFix":"// before\nmyWindow.Owner = myWindow; // ArgumentException\n// after\nif (myWindow != Application.Current.MainWindow) {\n    myWindow.Owner = Application.Current.MainWindow;\n}","handlingStrategy":"validation","validationCode":"if (!ReferenceEquals(owner, targetWindow)) { targetWindow.Owner = owner; }","typeGuard":"bool IsValidOwner(Window target, Window owner) => !ReferenceEquals(target, owner);","tryCatchPattern":"try { target.Owner = owner; } catch (ArgumentException ex) when (ex.Message == SR.CannotSetOwnerToItself) { /* pick a different owner or leave unowned */ }","preventionTips":["Never pass the dialog itself as its owner","Use Application.Current.MainWindow as a default owner","Assert ownership identity in helper methods"],"tags":["wpf","window","owner","argument"],"backgroundTag":"invalid-argument-value","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"}