{"record":{"id":"38a715dfc9305925","repo":"MaterialDesignInXAML/MaterialDesignInXamlToolkit","slug":"index","errorCode":null,"errorMessage":"index","messagePattern":"index","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/MaterialDesignThemes.Wpf/Internal/TreeListViewItemsCollection.cs","lineNumber":90,"sourceCode":"            throw new ArgumentOutOfRangeException(nameof(level), level, $\"Item level must not be more than one level greater the previous item ({previousItemLevel})\");\n        }\n\n        int nextItemLevel = index < Count ? ItemLevels[index] : 0;\n        if (level < nextItemLevel)\n        {\n            throw new ArgumentOutOfRangeException(nameof(level), level, $\"Item level must not be less than the level item after it ({nextItemLevel})\");\n        }\n\n        InternalInsertItem(index, item, level);\n        if (previousItemLevel >= 0 && previousItemLevel == level - 1)\n        {\n            ItemIsExpanded[index - 1] = true;\n        }\n    }\n\n    public object? GetParent(int index)\n    {\n        if (index < 0 || index >= Count) throw new ArgumentOutOfRangeException(nameof(index));\n        int level = ItemLevels[index];\n        if (level == 0) return null;\n        for (int i = index - 1; i >= 0; i--)\n        {\n            if (ItemLevels[i] == level - 1)\n            {\n                return this[i];\n            }\n        }\n        return null;\n    }\n\n    public IEnumerable<int> GetDirectChildrenIndexes(int index)\n    {\n        if (index < 0 || index >= Count) throw new ArgumentOutOfRangeException(nameof(index));\n\n        return GetDirectChildrenIndexesImplementation(index);\n","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/blob/98edec3a0b96ef272c587b14bcd67ef5f928a7ed/src/MaterialDesignThemes.Wpf/Internal/TreeListViewItemsCollection.cs#L72-L108","documentation":"GetParent walks backward from `index` to find the nearest ancestor at level-1. It validates that index is within [0, Count) and throws ArgumentOutOfRangeException(nameof(index)) otherwise.","triggerScenarios":"Calling GetParent(-1), GetParent(Count), or any index outside [0, Count).","commonSituations":"Using a stale index after a Reset/Remove without re-querying Count; off-by-one loop bounds; confusing the wrapped source-list index with the collection index.","solutions":["Bounds-check the index: `if (index < 0 || index >= collection.Count) ...` before calling.","Refresh indexes after any Add/Remove/Reset on the collection or its source.","Use the index returned by collection.IndexOf rather than the source list position."],"exampleFix":"// before\nvar parent = collection.GetParent(i);\n// after\nif (i >= 0 && i < collection.Count)\n    var parent = collection.GetParent(i);","handlingStrategy":"validation","validationCode":"if (index < 0 || index >= collection.Count)\n    throw new ArgumentOutOfRangeException(nameof(index));\nvar parent = collection.GetParent(index);","typeGuard":"index >= 0 && index < collection.Count","tryCatchPattern":null,"preventionTips":["Refresh indexes after Add/Remove/Reset on the collection or its source.","Use collection.IndexOf to obtain a valid index rather than the source-list position."],"tags":["wpf","tree-list-view","argument-out-of-range","hierarchy"],"backgroundTag":null,"analyzedSha":"98edec3a0b96ef272c587b14bcd67ef5f928a7ed","analyzedAt":"2026-08-13T14:31:19.111Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}