{"record":{"id":"ebe1969352d79cec","repo":"dotnet/wpf","slug":"sr-visual-notchild","errorCode":null,"errorMessage":"SR.Visual_NotChild","messagePattern":"SR\\.Visual_NotChild","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Visual.cs","lineNumber":2648,"sourceCode":"\n        /// <summary>\n        /// DisconnectChild\n        ///\n        ///    Derived classes must call this method to notify the Visual layer that a\n        ///    child was removed from the children collection. The Visual layer will then call\n        ///    GetChildren to find out which child has been removed.\n        ///\n        /// </summary>\n        protected void RemoveVisualChild(Visual child)\n        {\n            if (child == null || child._parent == null)\n            {\n                return;\n            }\n\n            if (child._parent != this)\n            {\n                throw new ArgumentException(SR.Visual_NotChild);\n            }\n\n            // invalid during a VisualTreeChanged event\n            VisualDiagnostics.VerifyVisualTreeChange(this);\n\n            VisualDiagnostics.OnVisualChildChanged(this, child, false);\n\n            if (InternalVisual2DOr3DChildrenCount == 0)\n            {\n                SetFlags(false, VisualFlags.HasChildren);\n            }\n\n            //\n            // Remove the child on all channels its current parent is marshalled to.\n            //\n\n            for (int i = 0; i < _proxy.Count; i++)\n            {","sourceCodeStart":2630,"sourceCodeEnd":2666,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Visual.cs#L2630-L2666","documentation":"Visual.RemoveVisualChild throws ArgumentException(SR.Visual_NotChild) when the specified child visual is not actually a child of this visual (its _parent differs from this). The method only removes direct children.","triggerScenarios":"Calling RemoveVisualChild with a visual whose parent is a different visual, or with a visual that was never added; stale references after the tree changed.","commonSituations":"Double-removal of a child; removing a grandchild from an ancestor; container recycling where the child was already re-parented.","solutions":["Verify VisualTreeHelper.GetParent(child) == this before removing","Guard removal with a parent check and skip if not a direct child","Track child addition/removal in custom containers to avoid duplicate removals"],"exampleFix":"// before\nparent.RemoveVisualChild(child);\n// after\nif (VisualTreeHelper.GetParent(child) == parent)\n    parent.RemoveVisualChild(child);","handlingStrategy":"validation","validationCode":"if (VisualTreeHelper.GetParent(child) != parent) return; // not a direct child","typeGuard":"bool IsDirectChild(Visual p, Visual c) => VisualTreeHelper.GetParent(c) == p;","tryCatchPattern":"try { parent.RemoveVisualChild(child); } catch (ArgumentException) { /* already removed or never a child */ }","preventionTips":["Guard removals with a parent check","Avoid duplicate removal calls; track removal state","Remove only from the immediate parent, not ancestors"],"tags":["wpf","visual-tree","not-a-child"],"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"}