{"record":{"id":"200d2f1c7dca8c62","repo":"dotnet/wpf","slug":"sr-alreadyhasparent","errorCode":null,"errorMessage":"SR.AlreadyHasParent","messagePattern":"SR\\.AlreadyHasParent","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/AnnotationObservableCollection.cs","lineNumber":110,"sourceCode":"        /// raises a CollectionChanged event to any listeners\n        /// </summary>\n        protected override void RemoveItem(int index)\n        {\n            T removedItem = this[index];\n\n            SetOwned(removedItem, false);\n\n            base.RemoveItem(index);\n        }\n\n        /// <summary>\n        /// called by base class Collection&lt;T&gt; when an item is added to list;\n        /// raises a CollectionChanged event to any listeners\n        /// </summary>\n        protected override void InsertItem(int index, T item)\n        {\n            if (ItemOwned(item))\n                throw new ArgumentException(SR.AlreadyHasParent);\n\n            base.InsertItem(index, item);\n\n            SetOwned(item, true);\n        }\n\n        /// <summary>\n        /// called by base class Collection&lt;T&gt; when an item is added to list;\n        /// raises a CollectionChanged event to any listeners\n        /// </summary>\n        protected override void SetItem(int index, T item)\n        {\n            if (ItemOwned(item))\n                throw new ArgumentException(SR.AlreadyHasParent);\n\n            T originalItem = this[index];\n\n            SetOwned(originalItem, false);","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/AnnotationObservableCollection.cs#L92-L128","documentation":"AnnotationObservableCollection<T>.InsertItem throws ArgumentException(SR.AlreadyHasParent) when the item being inserted is already owned by another AnnotationObservableCollection. Each annotation component/resource can belong to only one owning collection, enforced by the internal ownership ('owned') flag.","triggerScenarios":"Adding the same AnnotationComponentBase (e.g. AnnotationComponent, AnnotationResource) instance to a second collection, or re-adding an item already in this collection while ownership flags are still set.","commonSituations":"Copying annotation components between two AnnotationService/AnnotationStore collections; sharing one component across multiple viewers; cloning logic that reuses instances instead of creating new ones.","solutions":["Create a new component instance for the second collection instead of sharing the instance","Remove the item from its current collection before inserting it elsewhere","Deep-copy the annotation component if it must appear in multiple contexts"],"exampleFix":"// before\ncollectionB.Add(sharedComponent); // already owned by collectionA\n// after\ncollectionA.Remove(sharedComponent);\ncollectionB.Add(sharedComponent);","handlingStrategy":"validation","validationCode":"// only insert if the item is not already owned elsewhere\n// check by membership in the other collection or via a wrapper property\nif (!collectionB.Contains(component)) collectionB.Add(component);","typeGuard":null,"tryCatchPattern":"try { collection.Insert(index, component); }\ncatch (ArgumentException) { /* component already has a parent — clone or remove first */ }","preventionTips":["Treat annotation components as single-owner objects","Remove from the old collection before adding to a new one","Clone (deep-copy) components that must appear in multiple collections"],"tags":["wpf","annotations","collection","ownership"],"backgroundTag":"invalid-state-transition","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"}