{"record":{"id":"9ffc97006798ded9","repo":"dotnet/wpf","slug":"sr-visualcollection-entryinuse","errorCode":null,"errorMessage":"SR.VisualCollection_EntryInUse","messagePattern":"SR\\.VisualCollection_EntryInUse","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/VisualCollection.cs","lineNumber":337,"sourceCode":"            }\n            set\n            {\n                VerifyAPIReadWrite(value);\n\n                ArgumentOutOfRangeException.ThrowIfNegative(index);\n                ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, _size);\n\n                Visual child = _items[index];\n\n                if ((value == null) && (child != null))\n                {\n                    DisconnectChild(index);\n                }\n                else if (value != null)\n                {\n                    if (child != null)\n                    {\n                        throw new System.ArgumentException(SR.VisualCollection_EntryInUse);\n                    }\n                    if ((value._parent != null) // Only a visual that isn't a visual parent or\n                        || value.IsRootElement) // are a root node of a visual target can be set into the collection.\n                    {\n                        throw new System.ArgumentException(SR.VisualCollection_VisualHasParent);\n                    }\n\n                    ConnectChild(index, value);\n                }\n            }\n        }\n\n        /// <summary>\n        /// Sets the specified visual at the specified index into the child\n        /// collection. It also corrects the parent.\n        /// Note that the function requires that _item[index] == null and it\n        /// also requires that the passed in child is not connected to another Visual.\n        /// </summary>","sourceCodeStart":319,"sourceCodeEnd":355,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/VisualCollection.cs#L319-L355","documentation":"Thrown by the VisualCollection indexer setter when trying to replace a non-null child at an index with a different visual that is already attached elsewhere (has a parent or is a visual-tree root). WPF allows a visual to have only one parent; the slot check (child != null combined with value already parented) raises ArgumentException before VisualHasParent is evaluated for re-parenting rules.","triggerScenarios":"Assigning collection[index] = someVisual while (a) the current child at that index is non-null and (b) someVisual._parent is not null or someVisual.IsRootElement is true — i.e. swapping an existing child for a visual that is itself already connected to another tree.","commonSituations":"Moving a shared visual (e.g. an adorner or overlay element) into a second parent without detaching it first; templating code that swaps slot contents with recycled visuals still parented elsewhere.","solutions":["Detach the incoming visual from its current parent (remove it from its old parent's children) before assigning it to the new index.","Ensure IsRootElement visuals (visual targets/roots) are never assigned into a child slot.","Null the slot or use a fresh visual instance instead of a shared one.","Verify with VisualTreeHelper.GetParent(value) == null before the swap."],"exampleFix":"// before\noldPanel.Children.Remove(sharedVisual);\ncollection[index] = sharedVisual; // if collection[index] != null and shared still parented elsewhere -> EntryInUse\n// after\nvar oldOwner = VisualTreeHelper.GetParent(sharedVisual) as Visual;\n(oldOwner as UIElement)?.EnsureVisuals().Remove(sharedVisual); // detach from any tree first\nif (collection[index] == null)\n{\n    collection[index] = sharedVisual;\n}","handlingStrategy":"validation","validationCode":"if (collection[index] != null &&\n    (VisualTreeHelper.GetParent(value) != null || value.IsRootElement))\n    return; // detach 'value' from its current tree first\ncollection[index] = value;","typeGuard":"static bool IsAttachable(Visual v) => VisualTreeHelper.GetParent(v) == null && !v.IsRootElement;","tryCatchPattern":"try { collection[index] = value; }\ncatch (ArgumentException) { /* incoming visual still owned elsewhere */ }","preventionTips":["Detach the incoming visual from its old parent before replacing a non-null slot.","Never assign VisualTarget root visuals into child collections.","Don't share one visual instance between multiple parents.","Use Add/Remove pairs rather than raw indexer swaps when moving visuals."],"tags":["wpf","visual-collection","visual-parent","argument","reparenting"],"backgroundTag":"invalid-argument-value","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"}