{"record":{"id":"6039f8150ec8069d","repo":"dotnet/wpf","slug":"sr-visual-argumentoutofrange-grid","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/Grid.cs","lineNumber":371,"sourceCode":"\n        /// <summary>\n        ///   Derived class must implement to support Visual children. The method must return\n        ///    the child at the specified index. Index must be between 0 and GetVisualChildrenCount-1.\n        ///\n        ///    By default a Visual does not have any children.\n        ///\n        ///  Remark:\n        ///       During this virtual call it is not valid to modify the Visual tree.\n        /// </summary>\n        protected override Visual GetVisualChild(int index)\n        {\n            // because \"base.Count + 1\" for GridLinesRenderer\n            // argument checking done at the base class\n            if(index == base.VisualChildrenCount)\n            {\n                if (_gridLinesRenderer == null)\n                {\n                    throw new ArgumentOutOfRangeException(nameof(index), index, SR.Visual_ArgumentOutOfRange);\n                }\n                return _gridLinesRenderer;\n            }\n            else return base.GetVisualChild(index);\n        }\n\n        /// <summary>\n        ///  Derived classes override this property to enable the Visual code to enumerate\n        ///  the Visual children. Derived classes need to return the number of children\n        ///  from this method.\n        ///\n        ///    By default a Visual does not have any children.\n        ///\n        ///  Remark: During this virtual method the Visual tree must not be modified.\n        /// </summary>\n        protected override int VisualChildrenCount\n        {\n            //since GridLinesRenderer has not been added as a child, so we do not subtract","sourceCodeStart":353,"sourceCodeEnd":389,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Grid.cs#L353-L389","documentation":"Grid.GetVisualChild(index) throws ArgumentOutOfRangeException (SR.Visual_ArgumentOutOfRange) when index equals the base VisualChildrenCount while the internal _gridLinesRenderer is null. Grid exposes one extra visual child (the gridlines renderer) beyond its content children; asking for that extra index when no renderer exists is out of range.","triggerScenarios":"Calling GetVisualChild(VisualTreeHelper.GetChildrenCount(...)) or GetVisualChild(grid.VisualChildrenCount) on a Grid that has no gridlines renderer (ShowGridLines == false), asking for one past the last real child.","commonSituations":"Visual-tree walking utilities enumerating children with an off-by-one count; code that assumes the gridlines renderer always exists; tooling/debug visualizers iterating indices 0..Count inclusive.","solutions":["Use VisualTreeHelper.GetChildrenCount(grid) and iterate indices 0..count-1 — the count already accounts for whether the renderer exists.","Only request the extra index after confirming grid.ShowGridLines is true (which instantiates _gridLinesRenderer).","Wrap GetVisualChild in an index check against the grid's VisualChildrenCount."],"exampleFix":"// before\nvar child = grid.GetVisualChild(grid.VisualChildrenCount); // throws when no gridlines renderer\n// after\nif (grid.ShowGridLines && index == grid.VisualChildrenCount)\n{\n    var renderer = grid.GetVisualChild(index); // gridlines renderer\n}\nelse if (index < grid.VisualChildrenCount)\n{\n    var child = grid.GetVisualChild(index);\n}","handlingStrategy":"type-guard","validationCode":"int count = VisualTreeHelper.GetChildrenCount(grid); bool valid = index >= 0 && index < count;","typeGuard":"bool IsValidVisualIndex(Grid g, int i) => i >= 0 && i < VisualTreeHelper.GetChildrenCount(g);","tryCatchPattern":"try { var child = grid.GetVisualChild(index); }\ncatch (ArgumentOutOfRangeException) { /* index past last child; no gridlines renderer present */ }","preventionTips":["Use VisualTreeHelper.GetChildrenCount as the bound","Assume gridlines renderer exists only when ShowGridLines is true","Iterate with 0..count-1, never count itself"],"tags":["wpf","grid","visual-tree","argument-out-of-range","gridlines"],"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"}