AvaloniaUI/Avalonia · error · InvalidOperationException

The Style already has a parent.

Error message

The Style already has a parent.

What it means

A Style's resources can be owned by exactly one IResourceHost at a time. IResourceProvider.AddOwner throws InvalidOperationException if Owner is already set, preventing two hosts from concurrently subscribing to the same resource dictionary. This enforces single-ownership of the Style's resource subtree.

Source

Thrown at src/Avalonia.Base/Styling/StyleBase.cs:164

                    instance.MakeShared();
                    _sharedInstance = instance;
                }
            }

            ao.GetValueStore().AddFrame(instance);
            instance.ApplyAnimations(ao);
            return instance;
        }

        internal virtual void SetParent(StyleBase? parent) => Parent = parent;

        void IResourceProvider.AddOwner(IResourceHost owner)
        {
            owner = owner ?? throw new ArgumentNullException(nameof(owner));

            if (Owner != null)
            {
                throw new InvalidOperationException("The Style already has a parent.");
            }

            Owner = owner;
            _resources?.AddOwner(owner);
        }

        void IResourceProvider.RemoveOwner(IResourceHost owner)
        {
            owner = owner ?? throw new ArgumentNullException(nameof(owner));

            if (Owner == owner)
            {
                Owner = null;
                _resources?.RemoveOwner(owner);
            }
        }
    }
}

View on GitHub (pinned to 11c5427268)

Solutions

  1. Remove the style from its current owner first (e.g. control1.Styles.Remove(style)) before adding it to the new owner.
  2. Create a fresh Style instance for the new owner instead of reusing the same object.
  3. If you genuinely need the same resources in multiple places, share them via a ResourceDictionary referenced through MergedDictionaries rather than sharing the Style object.

Example fix

// before
control1.Styles.Add(sharedStyle);
control2.Styles.Add(sharedStyle); // InvalidOperationException: already has a parent

// after
control1.Styles.Remove(sharedStyle);
control2.Styles.Add(sharedStyle);
Defensive patterns

Strategy: validation

Validate before calling

// Ensure the style is not already owned before adding it to a new owner.
var rp = (IResourceProvider)style;
if (rp.Owner is not null)
{
    // detach from previous owner first (e.g. previousStyles.Remove(style))
}
newStyles.Add(style);

Type guard

static bool CanOwnStyle(IResourceProvider style) => style.Owner is null;

Try / catch

try { newStyles.Add(sharedStyle); }
catch (InvalidOperationException ex) when (ex.Message.Contains("already has a parent"))
{
    // detach from current owner and retry once
    previousStyles.Remove(sharedStyle);
    newStyles.Add(sharedStyle);
}

Prevention

When it happens

Trigger: Adding a Style that is still owned by another host to a second Styles collection without first removing it from the original owner. Owner is set by the previous AddOwner and only cleared when RemoveOwner is called with the matching owner (or the Styles collection removes the style).

Common situations: Dynamically moving a shared Style object between controls; reusing a Style instance across multiple windows or a global Application.Styles and a per-control Styles collection simultaneously; theming systems that pool and reassign Style instances.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/871222df7c474fed. Report an issue: GitHub.