{"record":{"id":"cdcea72a98c9e7a4","repo":"MaterialDesignInXAML/MaterialDesignInXamlToolkit","slug":"item-level-must-not-be-negative","errorCode":null,"errorMessage":"Item level must not be negative","messagePattern":"Item level must not be negative","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/MaterialDesignThemes.Wpf/Internal/TreeListViewItemsCollection.cs","lineNumber":66,"sourceCode":"                // We've have passed the provided index, which means we've found a non-prior root parentLevel item; bail out.\n                break;\n            }\n        }\n        return priorNonRootLevelItems;\n    }\n\n    public int GetLevel(int index)\n        => ItemLevels[index];\n\n    public bool GetIsExpanded(int index)\n        => ItemIsExpanded[index];\n\n    public void SetIsExpanded(int index, bool isExpanded)\n        => ItemIsExpanded[index] = isExpanded;\n\n    public void InsertWithLevel(int index, object? item, int level)\n    {\n        if (level < 0) throw new ArgumentOutOfRangeException(nameof(level), level, \"Item level must not be negative\");\n\n        //Always allowed to request previous item parentLevel + 1 as this is inserting a \"child\"\n        int previousItemLevel = index > 0 ? ItemLevels[index - 1] : -1;\n        if (level > previousItemLevel + 1)\n        {\n            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;","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/blob/98edec3a0b96ef272c587b14bcd67ef5f928a7ed/src/MaterialDesignThemes.Wpf/Internal/TreeListViewItemsCollection.cs#L48-L84","documentation":"TreeListViewItemsCollection flattens a hierarchy into (item, level) pairs. InsertWithLevel inserts an item at a given depth and rejects a negative level with ArgumentOutOfRangeException, since level 0 denotes a root and there is no valid negative depth.","triggerScenarios":"Calling collection.InsertWithLevel(index, item, level) with level < 0.","commonSituations":"Off-by-one math computing a child level (e.g. parentLevel - 1), or using -1 as a 'no parent' sentinel that leaks into the level argument.","solutions":["Clamp or validate: use Math.Max(0, computedLevel) before calling.","Use level 0 for root items; children are parentLevel + 1.","Add an explicit `if (level < 0) throw;` guard at the call site during debugging to catch the source of the bad value."],"exampleFix":"// before\ncollection.InsertWithLevel(i, child, parentLevel - 1);\n// after\ncollection.InsertWithLevel(i, child, parentLevel + 1);","handlingStrategy":"validation","validationCode":"if (level < 0) level = 0; // or throw at the call site\ncollection.InsertWithLevel(index, item, level);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Derive child levels as parentLevel + 1, never parentLevel - 1.","Treat 0 as the only valid root level.","Add an assertion guard while debugging tree-building code."],"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"}