dotnet/wpf · error · ArgumentException

SR.CannotSetOwnerToItself

Error message

SR.CannotSetOwnerToItself

What it means

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.

Solutions

  1. Guard with if (value != this) before assigning Owner
  2. Pass the correct owner window (e.g. Application.Current.MainWindow or the calling window), not the dialog itself
  3. Change helper signatures to never use the target window as its own owner
  4. Use WindowInteropHelper.SetOwner with a verified distinct parent handle when doing interop ownership

Example fix

// before
myWindow.Owner = myWindow; // ArgumentException
// after
if (myWindow != Application.Current.MainWindow) {
    myWindow.Owner = Application.Current.MainWindow;
}
Defensive patterns

Strategy: validation

Validate before calling

if (!ReferenceEquals(owner, targetWindow)) { targetWindow.Owner = owner; }

Type guard

bool IsValidOwner(Window target, Window owner) => !ReferenceEquals(target, owner);

Try / catch

try { target.Owner = owner; } catch (ArgumentException ex) when (ex.Message == SR.CannotSetOwnerToItself) { /* pick a different owner or leave unowned */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/c3818bfdc13391ad. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs:1228

        public Window Owner
        {
            get
            {
                // this call ends up throwing an exception if accessing Owner
                // is not allowed
                VerifyApiSupported();
                VerifyContextAndObjectState();
                return _ownerWindow;
            }
            set
            {
                // this call ends up throwing an exception if accessing Owner
                // is not allowed
                VerifyApiSupported();
                VerifyContextAndObjectState();
                if (value == this)
                {
                    throw new ArgumentException(SR.CannotSetOwnerToItself);
                }

                if (_showingAsDialog)
                {
                    throw new InvalidOperationException(SR.CantSetOwnerAfterDialogIsShown);
                }

                if (value != null && value.IsSourceWindowNull)
                {
                    // Try to be specific in the error message.
                    if (value._disposed)
                    {
                        throw new InvalidOperationException(SR.CantSetOwnerToClosedWindow);
                    }
                    throw new InvalidOperationException(SR.CantSetOwnerWhosHwndIsNotCreated);
                }

                if ( _ownerWindow == value )

View on GitHub (pinned to 81131a70a4)