{"record":{"id":"f567d6b20481afd8","repo":"MaterialDesignInXAML/MaterialDesignInXamlToolkit","slug":"item-level-must-not-be-less-than-the-level-item-af","errorCode":null,"errorMessage":"Item level must not be less than the level item after it ({nextItemLevel})","messagePattern":"Item level must not be less than the level item after it \\((.+?)\\)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/MaterialDesignThemes.Wpf/Internal/TreeListViewItemsCollection.cs","lineNumber":78,"sourceCode":"\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));\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            {","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/blob/98edec3a0b96ef272c587b14bcd67ef5f928a7ed/src/MaterialDesignThemes.Wpf/Internal/TreeListViewItemsCollection.cs#L60-L96","documentation":"An item may not be shallower than the item that follows it, because deeper items in a flat ordered list depend on a preceding ancestor at level-1. InsertWithLevel throws ArgumentOutOfRangeException when level < nextItemLevel, which would otherwise orphan the following subtree.","triggerScenarios":"Inserting a shallow (e.g. root-level) item immediately before an existing nested item whose level is greater, without relocating that subtree.","commonSituations":"Reordering or inserting a new parent above an already-nested subtree; moving a root node into the middle of a child group.","solutions":["Ensure the inserted level is >= the level of the item currently at that index, or move/relocate the deeper items first.","Insert new roots at the end of the collection (or after the last item at the same level) rather than mid-subtree.","When wrapping a hierarchical source, let the built-in ItemsSource_CollectionChanged handler manage insertion rather than calling InsertWithLevel directly."],"exampleFix":"// before: inserting a root (0) before a nested child (1)\ncollection.InsertWithLevel(2, newRoot, 0);\n// after: insert at the end of the sibling group\nint end = collection.Count;\ncollection.InsertWithLevel(end, newRoot, 0);","handlingStrategy":"validation","validationCode":"int next = index < collection.Count ? collection.GetLevel(index) : 0;\nif (level < next) throw new InvalidOperationException(\"Cannot insert above a deeper subtree.\");\ncollection.InsertWithLevel(index, item, level);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Insert new roots at the end of the collection or after the last same-level item.","Prefer the ItemsSource path over manual InsertWithLevel for hierarchy changes."],"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"}