{"record":{"id":"7b0756928b7dcc0d","repo":"dotnet/wpf","slug":"sr-visual-argumentoutofrange-scrollcontentpresenter","errorCode":null,"errorMessage":"SR.Visual_ArgumentOutOfRange","messagePattern":"SR\\.Visual_ArgumentOutOfRange","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/ScrollContentPresenter.cs","lineNumber":341,"sourceCode":"            {\n                // Four states make sense:\n                // 0 Children.  No Content or AdornerLayer.  Valid - do nothing.\n                // 2 Children.  Content is first child, AdornerLayer\n\n                // One for the base.TemplateChild and one for the _adornerlayer.\n                return (base.TemplateChild == null) ? 0 : 2;\n            }\n        }\n\n        /// <summary>\n        /// Returns the child at the specified index.\n        /// </summary>\n        protected override Visual GetVisualChild(int index)\n        {\n            //check if there is a TemplateChild on FrameworkElement\n            if (base.TemplateChild == null)\n            {\n                throw new ArgumentOutOfRangeException(nameof(index), index, SR.Visual_ArgumentOutOfRange);\n            }\n            else\n            {\n                switch (index)\n                {\n                    case 0:\n                        return base.TemplateChild;\n\n                    case 1:\n                        return _adornerLayer;\n\n                    default:\n                        throw new ArgumentOutOfRangeException(nameof(index), index, SR.Visual_ArgumentOutOfRange);\n                }\n            }\n         }\n\n        /// <summary>","sourceCodeStart":323,"sourceCodeEnd":359,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/ScrollContentPresenter.cs#L323-L359","documentation":"ScrollContentPresenter.GetVisualChild supports at most two children: its TemplateChild (index 0) and the adorner layer (index 1). If TemplateChild is null, even index 0 is invalid, so it throws ArgumentOutOfRangeException with SR.Visual_ArgumentOutOfRange. The presenter's visual tree is missing its expected content part.","triggerScenarios":"Querying visual children of a ScrollContentPresenter whose template child (the scroll content) hasn't been applied yet — e.g. before the template is instantiated — or requesting an index when the presenter has no content.","commonSituations":"Automation or hit-testing code walking visuals before template application (pre-Loaded), custom templates for ScrollViewer that omit the required content part, tests inspecting visuals in constructor.","solutions":["Delay visual-tree inspection until the element's template is applied (Loaded event or TemplateApplied/OnApplyTemplate).","Check ScrollContentPresenter.TemplateChild != null (or GetChildrenCount > 0) before calling GetVisualChild(0).","If using a custom ScrollViewer template, ensure it contains the required part that becomes the presenter's TemplateChild."],"exampleFix":"// before\nvar content = VisualTreeHelper.GetChild(scrollContentPresenter, 0); // throws if TemplateChild null\n// after\nif (VisualTreeHelper.GetChildrenCount(scrollContentPresenter) > 0)\n    var content = VisualTreeHelper.GetChild(scrollContentPresenter, 0);","handlingStrategy":"validation","validationCode":"if (presenter.TemplateChild == null || VisualTreeHelper.GetChildrenCount(presenter) == 0) return;","typeGuard":"bool PresenterHasContent(ScrollContentPresenter p) => p.TemplateChild != null;","tryCatchPattern":"try { var c = VisualTreeHelper.GetChild(presenter, 0); }\ncatch (ArgumentOutOfRangeException) { /* template not applied yet */ }","preventionTips":["Inspect visuals only after OnApplyTemplate/Loaded","Check GetChildrenCount before indexing","Ensure custom ScrollViewer templates include required parts"],"tags":["wpf","scrollviewer","visual-tree","argument-out-of-range"],"backgroundTag":"index-out-of-range","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}