{"record":{"id":"253d003b05d13c3d","repo":"dotnet/wpf","slug":"sr-stack-visualindifferentsubtree","errorCode":null,"errorMessage":"SR.Stack_VisualInDifferentSubTree","messagePattern":"SR\\.Stack_VisualInDifferentSubTree","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Stack.cs","lineNumber":852,"sourceCode":"                    : ((UIElement)children[i]).DesiredSize.Height;\n            }\n\n            return physicalOffset;\n        }\n\n        private int FindChildIndexThatParentsVisual(Visual child)\n        {\n            DependencyObject dependencyObjectChild = child;\n\n            DependencyObject parent = VisualTreeHelper.GetParent(child);\n\n            while (parent != this)\n            {\n                dependencyObjectChild = parent;\n                parent = VisualTreeHelper.GetParent(dependencyObjectChild);\n                if (parent == null)\n                {\n                    throw new ArgumentException(SR.Stack_VisualInDifferentSubTree,nameof(child));\n                }\n            }\n\n            UIElementCollection children = this.Children;\n            //The Downcast is ok because StackPanel's\n            //child has to be a UIElement to be in this.Children collection\n            return (children.IndexOf((UIElement)dependencyObjectChild));\n        }\n\n        private void MakeVisiblePhysicalHelper(Rect r, ref Vector newOffset, ref Rect newRect)\n        {\n            double viewportOffset;\n            double viewportSize;\n            double targetRectOffset;\n            double targetRectSize;\n            double minPhysicalOffset;\n\n            bool fHorizontal = (Orientation == Orientation.Horizontal);","sourceCodeStart":834,"sourceCodeEnd":870,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Stack.cs#L834-L870","documentation":"StackPanel.MeasureOverride walks up the visual tree from a child to verify it belongs to the same visual subtree as the panel. If VisualTreeHelper.GetParent returns null before reaching the panel itself, the child is in a different visual subtree, and an ArgumentException (Stack_VisualInDifferentSubTree) is thrown for the child parameter.","triggerScenarios":"A UIElement reports a child that is not actually its visual descendant — typically with custom panels/elements that yield children not connected to the StackPanel's visual tree, or elements whose visual parent chain is broken/re-parented during measure.","commonSituations":"Custom controls overriding GetVisualChildren incorrectly; attaching elements created in a different visual root (popup/window) into a StackPanel's measure pass; virtualization/custom layout bugs where children are temporarily disconnected.","solutions":["Ensure all children yielded to measure are visual children of the StackPanel (AddVisualChild if needed).","Fix custom panel/control child enumeration to return only connected visuals.","Avoid inserting elements from a different visual root; re-parent them first (remove from old parent before adding).","Reproduce in a minimal sample and check the parent chain with VisualTreeHelper.GetParent before measure."],"exampleFix":"// before\npanel.Children.Add(elementFromOtherWindow); // still parented elsewhere\n// after\n((Panel)elementFromOtherWindow.Parent).Children.Remove(elementFromOtherWindow);\npanel.Children.Add(elementFromOtherWindow);","handlingStrategy":"type-guard","validationCode":"bool inSameSubtree = false;\nDependencyObject p = child;\nwhile (p != null) { if (p == panel) { inSameSubtree = true; break; } p = VisualTreeHelper.GetParent(p); }\nif (!inSameSubtree) throw new ArgumentException(\"child not in panel's visual subtree\");","typeGuard":"bool IsVisualDescendant(DependencyObject child, DependencyObject ancestor) {\n  for (var p = child; p != null; p = VisualTreeHelper.GetParent(p))\n    if (p == ancestor) return true;\n  return false;\n}","tryCatchPattern":null,"preventionTips":["Call RemoveVisualChild/Remove from old parent before re-parenting elements.","Ensure custom controls report only connected visual children.","Verify parent chains with VisualTreeHelper when reusing elements across trees.","Never insert popup/window-owned elements directly into another subtree."],"tags":["wpf","layout","visual-tree"],"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"}