{"record":{"id":"871222df7c474fed","repo":"AvaloniaUI/Avalonia","slug":"the-style-already-has-a-parent","errorCode":null,"errorMessage":"The Style already has a parent.","messagePattern":"The Style already has a parent\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Styling/StyleBase.cs","lineNumber":164,"sourceCode":"                    instance.MakeShared();\n                    _sharedInstance = instance;\n                }\n            }\n\n            ao.GetValueStore().AddFrame(instance);\n            instance.ApplyAnimations(ao);\n            return instance;\n        }\n\n        internal virtual void SetParent(StyleBase? parent) => Parent = parent;\n\n        void IResourceProvider.AddOwner(IResourceHost owner)\n        {\n            owner = owner ?? throw new ArgumentNullException(nameof(owner));\n\n            if (Owner != null)\n            {\n                throw new InvalidOperationException(\"The Style already has a parent.\");\n            }\n\n            Owner = owner;\n            _resources?.AddOwner(owner);\n        }\n\n        void IResourceProvider.RemoveOwner(IResourceHost owner)\n        {\n            owner = owner ?? throw new ArgumentNullException(nameof(owner));\n\n            if (Owner == owner)\n            {\n                Owner = null;\n                _resources?.RemoveOwner(owner);\n            }\n        }\n    }\n}","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Styling/StyleBase.cs#L146-L182","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Remove the style from its current owner first (e.g. control1.Styles.Remove(style)) before adding it to the new owner.","Create a fresh Style instance for the new owner instead of reusing the same object.","If you genuinely need the same resources in multiple places, share them via a ResourceDictionary referenced through MergedDictionaries rather than sharing the Style object."],"exampleFix":"// before\ncontrol1.Styles.Add(sharedStyle);\ncontrol2.Styles.Add(sharedStyle); // InvalidOperationException: already has a parent\n\n// after\ncontrol1.Styles.Remove(sharedStyle);\ncontrol2.Styles.Add(sharedStyle);","handlingStrategy":"validation","validationCode":"// Ensure the style is not already owned before adding it to a new owner.\nvar rp = (IResourceProvider)style;\nif (rp.Owner is not null)\n{\n    // detach from previous owner first (e.g. previousStyles.Remove(style))\n}\nnewStyles.Add(style);","typeGuard":"static bool CanOwnStyle(IResourceProvider style) => style.Owner is null;","tryCatchPattern":"try { newStyles.Add(sharedStyle); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"already has a parent\"))\n{\n    // detach from current owner and retry once\n    previousStyles.Remove(sharedStyle);\n    newStyles.Add(sharedStyle);\n}","preventionTips":["Treat Style instances as single-owner; do not share one object across multiple Styles collections.","Always remove a style from its current owner before adding it elsewhere.","For shared resources, prefer MergedDictionaries over sharing Style objects.","Avoid pooling and reusing Style instances across windows without explicit detach."],"tags":["avalonia","styling","resources","ownership","invalid-operation"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}