{"record":{"id":"f255479fda0628fb","repo":"MaterialDesignInXAML/MaterialDesignInXamlToolkit","slug":"item-level-must-not-be-more-than-one-level-greater","errorCode":null,"errorMessage":"Item level must not be more than one level greater the previous item ({previousItemLevel})","messagePattern":"Item level must not be more than one level greater the previous item \\((.+?)\\)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/MaterialDesignThemes.Wpf/Internal/TreeListViewItemsCollection.cs","lineNumber":72,"sourceCode":"\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;\n        }\n    }\n\n    public object? GetParent(int index)\n    {\n        if (index < 0 || index >= Count) throw new ArgumentOutOfRangeException(nameof(index));","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/blob/98edec3a0b96ef272c587b14bcd67ef5f928a7ed/src/MaterialDesignThemes.Wpf/Internal/TreeListViewItemsCollection.cs#L54-L90","documentation":"Because the collection stores items as a flat ordered list with per-item levels, an inserted item may be at most one level deeper than the preceding item (its parent). InsertWithLevel throws ArgumentOutOfRangeException when level > previousItemLevel + 1, since a deeper level would create a gap with no ancestor at the intermediate level.","triggerScenarios":"Inserting an item whose level is two or more greater than the previous item's level (e.g. previous item is level 0 and you pass level 2).","commonSituations":"Inserting a grandchild directly after a root without first inserting the parent, or miscomputing the level after a drag-and-drop reorder.","solutions":["Insert the intermediate parent first so the previous item is exactly one level shallower than the new item.","Clamp the new level to previousItemLevel + 1 when inserting as a child.","Re-derive the level from the model's parent chain rather than computing it ad hoc."],"exampleFix":"// before (previous item is root, level 0)\ncollection.InsertWithLevel(i, grandchild, 2);\n// after (insert parent at level 1 first, then grandchild)\ncollection.InsertWithLevel(i, parent, 1);\ncollection.InsertWithLevel(i + 1, grandchild, 2);","handlingStrategy":"validation","validationCode":"int prev = index > 0 ? collection.GetLevel(index - 1) : -1;\nif (level > prev + 1) level = prev + 1;\ncollection.InsertWithLevel(index, item, level);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Insert intermediate parents before deeper descendants.","Compute insert level from the model parent chain rather than a counter."],"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"}