{"record":{"id":"b4e4b2b5954a5a2c","repo":"lepoco/wpfui","slug":"the-argument-nameof-index-must-be-0-and-th","errorCode":null,"errorMessage":"The argument {nameof(index)} must be >= 0 and < the number of items.","messagePattern":"The argument (.+?) must be >= 0 and < the number of items\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Wpf.Ui/Controls/VirtualizingWrapPanel/VirtualizingWrapPanel.cs","lineNumber":495,"sourceCode":"    }\n\n    /// <summary>\n    /// Gets item row index.\n    /// </summary>\n    private int GetRowIndex(double location)\n    {\n        var calculatedRowIndex = (int)Math.Floor(location / GetHeight(ChildSize));\n        var maxRowIndex = (int)Math.Ceiling((double)Items.Count / (double)ItemsPerRowCount);\n\n        return Math.Max(Math.Min(calculatedRowIndex, maxRowIndex), 0);\n    }\n\n    /// <inheritdoc />\n    protected override void BringIndexIntoView(int index)\n    {\n        if (index < 0 || index >= Items.Count)\n        {\n            throw new ArgumentOutOfRangeException(\n                nameof(index),\n                $\"The argument {nameof(index)} must be >= 0 and < the number of items.\"\n            );\n        }\n\n        if (ItemsPerRowCount == 0)\n        {\n            throw new InvalidOperationException();\n        }\n\n        var offset = (index / ItemsPerRowCount) * GetHeight(ChildSize);\n\n        if (Orientation == Orientation.Horizontal)\n        {\n            SetHorizontalOffset(offset);\n        }\n        else\n        {","sourceCodeStart":477,"sourceCodeEnd":513,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/Controls/VirtualizingWrapPanel/VirtualizingWrapPanel.cs#L477-L513","documentation":"VirtualizingWrapPanel.BringIndexIntoView is the WPF virtualisation contract method called by the framework (and ItemsControl bring-into-view logic) to scroll a given item index into view. It validates that index is within [0, Items.Count) and throws ArgumentOutOfRangeException otherwise. This guards against the panel trying to compute an offset for a row that does not exist.","triggerScenarios":"The framework or user code calls BringIndexIntoView with an index that is negative or >= the current Items.Count - e.g. after the source collection changed but before the panel's Items was synchronised, or a stale index retained from a previous larger collection.","commonSituations":"Collection is replaced/reset between selecting an item and scrolling it into view; bound source filtered down so a previously valid index is now out of range; off-by-one when computing the last index; calling BringIndexIntoView on an empty list.","solutions":["Bounds-check against the live collection count before calling BringIndexIntoView.","Re-resolve the index from the item (e.g. via IndexOf) right before scrolling, after any collection reset.","Avoid scrolling to an index during collection-change notifications; defer to the dispatcher."],"exampleFix":"// before\npanel.BringIndexIntoView(selectedIndex);\n\n// after\nif (selectedIndex >= 0 && selectedIndex < items.Count)\n{\n    panel.BringIndexIntoView(selectedIndex);\n}","handlingStrategy":"validation","validationCode":"if (index < 0 || index >= items.Count)\n{\n    throw new ArgumentOutOfRangeException(nameof(index));\n}\npanel.BringIndexIntoView(index);","typeGuard":"static bool IsInViewRange(int index, int count) => index >= 0 && index < count;","tryCatchPattern":"try { panel.BringIndexIntoView(index); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"index\")\n{\n    _logger.LogDebug(ex, \"Stale index {Index} no longer in range.\", index);\n}","preventionTips":["Re-resolve the index from the live collection right before scrolling.","Defer bring-into-view calls until after collection-change processing completes.","Bounds-check whenever the source may have been filtered or replaced."],"tags":["virtualization","wrap-panel","argument-out-of-range","wpf"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}