MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · ArgumentOutOfRangeException
Item level must not be less than the level item after it ({n
Error message
Item level must not be less than the level item after it ({nextItemLevel}) What it means
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.
Source
Thrown at src/MaterialDesignThemes.Wpf/Internal/TreeListViewItemsCollection.cs:78
public void SetIsExpanded(int index, bool isExpanded)
=> ItemIsExpanded[index] = isExpanded;
public void InsertWithLevel(int index, object? item, int level)
{
if (level < 0) throw new ArgumentOutOfRangeException(nameof(level), level, "Item level must not be negative");
//Always allowed to request previous item parentLevel + 1 as this is inserting a "child"
int previousItemLevel = index > 0 ? ItemLevels[index - 1] : -1;
if (level > previousItemLevel + 1)
{
throw new ArgumentOutOfRangeException(nameof(level), level, $"Item level must not be more than one level greater the previous item ({previousItemLevel})");
}
int nextItemLevel = index < Count ? ItemLevels[index] : 0;
if (level < nextItemLevel)
{
throw new ArgumentOutOfRangeException(nameof(level), level, $"Item level must not be less than the level item after it ({nextItemLevel})");
}
InternalInsertItem(index, item, level);
if (previousItemLevel >= 0 && previousItemLevel == level - 1)
{
ItemIsExpanded[index - 1] = true;
}
}
public object? GetParent(int index)
{
if (index < 0 || index >= Count) throw new ArgumentOutOfRangeException(nameof(index));
int level = ItemLevels[index];
if (level == 0) return null;
for (int i = index - 1; i >= 0; i--)
{
if (ItemLevels[i] == level - 1)
{View on GitHub (pinned to 98edec3a0b)
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.
Example fix
// before: inserting a root (0) before a nested child (1) collection.InsertWithLevel(2, newRoot, 0); // after: insert at the end of the sibling group int end = collection.Count; collection.InsertWithLevel(end, newRoot, 0);
Defensive patterns
Strategy: validation
Validate before calling
int next = index < collection.Count ? collection.GetLevel(index) : 0;
if (level < next) throw new InvalidOperationException("Cannot insert above a deeper subtree.");
collection.InsertWithLevel(index, item, level); Prevention
- 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.
When it happens
Trigger: Inserting a shallow (e.g. root-level) item immediately before an existing nested item whose level is greater, without relocating that subtree.
Common situations: Reordering or inserting a new parent above an already-nested subtree; moving a root node into the middle of a child group.
Related errors
- Item level must not be negative
- Item level must not be more than one level greater the previ
- index
- Value must be positive
- Password cannot be empty
AI-assisted analysis of MaterialDesignInXAML/MaterialDesignInXamlToolkit@98edec3a0b (2026-08-13).
Data as JSON: /api/errors/f567d6b20481afd8.
Report an issue: GitHub.