{"record":{"id":"eaf0bf5f602a4130","repo":"AvaloniaUI/Avalonia","slug":"the-resourcedictionary-already-has-a-parent","errorCode":null,"errorMessage":"The ResourceDictionary already has a parent.","messagePattern":"The ResourceDictionary already has a parent\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Controls/ResourceProvider.cs","lineNumber":83,"sourceCode":"    /// Handles when owner was removed.\n    /// Base method implementation raises <see cref=\"IResourceHost.NotifyHostedResourcesChanged\"/>, if this provider has any resources.\n    /// </summary>\n    /// <param name=\"owner\">Old owner.</param>\n    protected virtual void OnRemoveOwner(IResourceHost owner)\n    {\n        if (HasResources)\n        {\n            owner.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Create());\n        }\n    }\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 ResourceDictionary already has a parent.\");\n        }\n\n        Owner = owner;\n\n        OnAddOwner(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\n            OnRemoveOwner(owner);\n        }\n    }","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Controls/ResourceProvider.cs#L65-L101","documentation":"Thrown by ResourceProvider.AddOwner (explicit IResourceProvider implementation) when the ResourceDictionary already has an Owner assigned. A resource provider is single-owner: it can be attached to exactly one IResourceHost. Attempting to add it to a second host without first removing the previous owner is rejected.","triggerScenarios":"Calling IResourceProvider.AddOwner(hostB) when Owner is already hostA (non-null). Happens when the same ResourceDictionary/ResourceProvider instance is shared across two controls/styles without being removed from the first.","commonSituations":"A single ResourceDictionary static instance is assigned to multiple Style.ResourceDictionaries or controls. Reusing a cached ResourceProvider across multiple windows without detaching. Programmatic ownership transfer that forgets RemoveOwner before AddOwner.","solutions":["Call IResourceProvider.RemoveOwner(currentOwner) before adding the resource provider to a new owner.","Create a separate ResourceDictionary instance per host rather than sharing one.","If sharing read-only resources, clone or wrap them so each host owns its own provider."],"exampleFix":"// before\nprovider.AddOwner(hostA);\nprovider.AddOwner(hostB); // throws — already owned\n// after\nprovider.RemoveOwner(hostA);\nprovider.AddOwner(hostB);","handlingStrategy":"validation","validationCode":"if (provider is IResourceProvider rp)\n{\n    // detach from previous owner first (check via reflection or known owner)\n    if (CurrentOwner != null) rp.RemoveOwner(CurrentOwner);\n    rp.AddOwner(newOwner);\n}","typeGuard":null,"tryCatchPattern":"try { ((IResourceProvider)provider).AddOwner(newOwner); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"already has a parent\"))\n{ /* call RemoveOwner on the current owner first, then retry */ }","preventionTips":["Always call RemoveOwner(currentOwner) before AddOwner on a shared provider.","Prefer a separate ResourceDictionary instance per host.","Track ownership when caching/sharing resource providers."],"tags":["resource-provider","ownership","resource-dictionary","styling"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}