dotnet/wpf · error · ArgumentException

SR.AlreadyHasParent

Error message

SR.AlreadyHasParent

What it means

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.

Solutions

  1. Create a new component instance for the second collection instead of sharing the instance
  2. Remove the item from its current collection before inserting it elsewhere
  3. Deep-copy the annotation component if it must appear in multiple contexts

Example fix

// before
collectionB.Add(sharedComponent); // already owned by collectionA
// after
collectionA.Remove(sharedComponent);
collectionB.Add(sharedComponent);
Defensive patterns

Strategy: validation

Validate before calling

// only insert if the item is not already owned elsewhere
// check by membership in the other collection or via a wrapper property
if (!collectionB.Contains(component)) collectionB.Add(component);

Try / catch

try { collection.Insert(index, component); }
catch (ArgumentException) { /* component already has a parent — clone or remove first */ }

Prevention

When it happens

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

Common situations: Copying annotation components between two AnnotationService/AnnotationStore collections; sharing one component across multiple viewers; cloning logic that reuses instances instead of creating new ones.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/AnnotationObservableCollection.cs:110

        /// raises a CollectionChanged event to any listeners
        /// </summary>
        protected override void RemoveItem(int index)
        {
            T removedItem = this[index];

            SetOwned(removedItem, false);

            base.RemoveItem(index);
        }

        /// <summary>
        /// called by base class Collection&lt;T&gt; when an item is added to list;
        /// raises a CollectionChanged event to any listeners
        /// </summary>
        protected override void InsertItem(int index, T item)
        {
            if (ItemOwned(item))
                throw new ArgumentException(SR.AlreadyHasParent);

            base.InsertItem(index, item);

            SetOwned(item, true);
        }

        /// <summary>
        /// called by base class Collection&lt;T&gt; when an item is added to list;
        /// raises a CollectionChanged event to any listeners
        /// </summary>
        protected override void SetItem(int index, T item)
        {
            if (ItemOwned(item))
                throw new ArgumentException(SR.AlreadyHasParent);

            T originalItem = this[index];

            SetOwned(originalItem, false);

View on GitHub (pinned to 81131a70a4)