{"record":{"id":"e67214893481adad","repo":"dotnet/wpf","slug":"sr-visual-argumentoutofrange-adornedelementplaceholder","errorCode":null,"errorMessage":"SR.Visual_ArgumentOutOfRange","messagePattern":"SR\\.Visual_ArgumentOutOfRange","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/AdornedElementPlaceholder.cs","lineNumber":142,"sourceCode":"        /// <summary>\n        /// Gets the Visual children count.\n        /// </summary>\n        protected override int VisualChildrenCount\n        {\n            get\n            {\n                return (_child == null) ? 0 : 1;\n            }\n        }\n\n        /// <summary>\n        /// Gets the Visual child at the specified index.\n        /// </summary>\n        protected override Visual GetVisualChild(int index)\n        {\n            if (_child == null || index != 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(index), index, SR.Visual_ArgumentOutOfRange);\n            }\n            return _child;\n        }        \n\n        /// <summary>\n        /// Returns enumerator to logical children.\n        /// </summary>\n        protected internal override IEnumerator LogicalChildren\n        {\n            get\n            {\n                // Could optimize this code by returning EmptyEnumerator.Instance if _child == null.\n                return new SingleChildEnumerator(_child);\n            }\n        }\n\n        /// <summary>\n        ///     This virtual method in called when IsInitialized is set to true and it raises an Initialized event","sourceCodeStart":124,"sourceCodeEnd":160,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/AdornedElementPlaceholder.cs#L124-L160","documentation":"AdornedElementPlaceholder.GetVisualChild throws ArgumentOutOfRangeException when there is no child visual (_child == null) or the requested index is not 0. The control exposes at most one visual child (its Content), so index 0 is the only valid index and only when a child has been set.","triggerScenarios":"Calling GetVisualChild(0) when the placeholder's Child/Content is null, or calling GetVisualChild with any index other than 0 (e.g. iterating with VisualTreeHelper.GetChild past index 0, or before the template content is applied).","commonSituations":"Walking the visual tree with VisualTreeHelper.GetChild on a ControlTemplate that hasn't set the placeholder's content yet; using VisualChildrenCount/GetVisualChild in a custom layout loop with a hardcoded child count; hitting the placeholder before ControlTemplate application completes.","solutions":["Check VisualChildrenCount before calling GetVisualChild and skip the placeholder when it is 0","Only request index 0; the placeholder has at most one visual child","Ensure the placeholder is used inside a ControlTemplate with content set before walking its tree"],"exampleFix":"// before\nvar child = VisualTreeHelper.GetChild(placeholder, i);\n// after\nif (i < VisualTreeHelper.GetChildrenCount(placeholder))\n{\n    var child = VisualTreeHelper.GetChild(placeholder, i);\n}","handlingStrategy":"validation","validationCode":"if (VisualTreeHelper.GetChildrenCount(placeholder) > index)\n    var child = VisualTreeHelper.GetChild(placeholder, index);\n","typeGuard":"bool HasVisualChild(AdornedElementPlaceholder p) => VisualTreeHelper.GetChildrenCount(p) > 0;\n","tryCatchPattern":null,"preventionTips":["Use VisualTreeHelper.GetChildrenCount before indexing","Never iterate a placeholder's visuals beyond count","Ensure template content applied before tree walking"],"tags":["wpf","argument-out-of-range","visual-tree"],"backgroundTag":"argument-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"}