{"record":{"id":"d45682deccf7e02a","repo":"dotnet/wpf","slug":"sr-circularownerchild","errorCode":null,"errorMessage":"SR.CircularOwnerChild","messagePattern":"SR\\.CircularOwnerChild","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs","lineNumber":1262,"sourceCode":"                }\n\n                if ( _ownerWindow == value )\n                {\n                    return;\n                }\n\n                if (!_disposed)\n                {\n                    // Check to see if value is already a child of this window.\n                    // If yes, throw Exception\n                    if (value != null)\n                    {\n                        WindowCollection ownedWindows = OwnedWindows;\n                        for (int i = 0; i < ownedWindows.Count; i++)\n                        {\n                            if (ownedWindows[i] == value)\n                            {\n                                throw new ArgumentException(SR.Format(SR.CircularOwnerChild, value, this));\n                            }\n                        }\n                    }\n\n                    // Update OwnerWindows of the previous owner\n                    // using OwnedWindowsInternl b/c we want to modifying the\n                    // underlying collection\n                    _ownerWindow?.OwnedWindowsInternal.Remove(this);\n                }\n\n                // Update parent handle. If value is null, then make parent\n                // handle IntPtr.Zero\n                _ownerWindow = value;\n\n                // We should not do anything if the window is already closed and maybe throw exception.\n                // In Dev10, it is unknown whether we can begin to throw exceptions, because it is a BC.\n                // In Dev10, we still update _ownerWindow after window is closed just so that the Owner getter\n                // returns the right value.","sourceCodeStart":1244,"sourceCodeEnd":1280,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs#L1244-L1280","documentation":"Window.Owner setter throws ArgumentException when the new owner is one of the windows this window currently owns (a child in OwnedWindows). Making a window's own child its owner would create a circular owner/owned relationship, which WPF forbids. The message includes both window instances.","triggerScenarios":"Setting A.Owner = B when B is already owned by A (A owns B, then B tries to own A), detected by scanning OwnedWindows for the value.","commonSituations":"Swapping owner/owned relationships dynamically; accidentally assigning the same window reference incorrectly in factory code that wires up many windows.","solutions":["Choose a third window (or null) as the mutual owner instead of reversing the relationship.","Clear the existing relationship first (set the child's Owner to null) before re-parenting.","Guard the assignment with a check that value is not in this.OwnedWindows."],"exampleFix":"// before\nchildA.Owner = mainWindow;\nmainWindow.Owner = childA; // circular\n// after\nchildA.Owner = mainWindow; // keep one-directional ownership","handlingStrategy":"validation","validationCode":"bool isNotOwnedChild(Window self, Window candidate) => candidate == null || !self.OwnedWindows.OfType<Window>().Contains(candidate);","typeGuard":null,"tryCatchPattern":"try { w.Owner = candidate; }\ncatch (ArgumentException) { candidate.Owner = null; w.Owner = candidate; }","preventionTips":["Model ownership as a strict tree, never cycles.","Before re-parenting, explicitly clear existing Owner.","Add debug asserts checking OwnedWindows before assignment."],"tags":["wpf","window","owner","circular-reference"],"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"}