{"record":{"id":"09922a1cd78c9850","repo":"dotnet/wpf","slug":"sr-visualcollection-visualhasparent","errorCode":null,"errorMessage":"SR.VisualCollection_VisualHasParent","messagePattern":"SR\\.VisualCollection_VisualHasParent","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/VisualCollection.cs","lineNumber":342,"sourceCode":"                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>\n        /// <exception cref=\"ArgumentException\">If the new child has already a parent or if the slot a the specified index is not null.</exception>\n        private void ConnectChild(int index, Visual value)\n        {\n            //\n            // -- Approved By The Core Team --","sourceCodeStart":324,"sourceCodeEnd":360,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/VisualCollection.cs#L324-L360","documentation":"Thrown by the VisualCollection indexer setter (and Add) when the visual being inserted already has a parent (_parent != null) or is the root element of a visual target (IsRootElement). A Visual can belong to exactly one visual tree, so WPF refuses to attach an already-parented visual rather than silently re-parenting it.","triggerScenarios":"collection[index] = v or collection.Add(v) where v._parent is non-null (v is a child of another Visual) or v.IsRootElement is true (v is the root of a VisualTarget/host hierarchy).","commonSituations":"Reusing one visual instance across multiple containers (movement between Grids/Panels without removal); adding a VisualTarget root visual into a normal children collection; forgetting that Remove must precede Add when moving a control.","solutions":["Remove the visual from its current parent's collection before adding it to the new one.","Create a new instance for the second location instead of sharing one visual.","For cross-tree display, use separate visuals or HwndHost/VisualTarget patterns that don't require dual parenting.","Guard with VisualTreeHelper.GetParent(v) == null && !v.IsRootElement before adding."],"exampleFix":"// before\n// sharedBorder currently inside panelA\npanelB.Children.Add(sharedBorder); // ArgumentException: VisualHasParent\n// after\npanelA.Children.Remove(sharedBorder);\npanelB.Children.Add(sharedBorder);","handlingStrategy":"validation","validationCode":"if (VisualTreeHelper.GetParent(visual) != null || visual.IsRootElement)\n    throw new InvalidOperationException(\"Detach the visual before re-parenting\");\ncollection[index] = visual;","typeGuard":"static bool IsOrphanVisual(Visual v) => VisualTreeHelper.GetParent(v) == null && !v.IsRootElement;","tryCatchPattern":"try { collection.Add(visual); }\ncatch (ArgumentException) { /* visual already has a parent or is a root */ }","preventionTips":["Remove before Add whenever moving a visual between containers.","Check VisualTreeHelper.GetParent(v) before attaching.","Treat IsRootElement visuals as unattachable.","Prefer creating fresh visuals over reparenting shared ones."],"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"}