MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · ArgumentOutOfRangeException

Item level must not be more than one level greater the previ

Error message

Item level must not be more than one level greater the previous item ({previousItemLevel})

What it means

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.

Source

Thrown at src/MaterialDesignThemes.Wpf/Internal/TreeListViewItemsCollection.cs:72

    public int GetLevel(int index)
        => ItemLevels[index];

    public bool GetIsExpanded(int index)
        => ItemIsExpanded[index];

    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));

View on GitHub (pinned to 98edec3a0b)

Solutions

  1. Insert the intermediate parent first so the previous item is exactly one level shallower than the new item.
  2. Clamp the new level to previousItemLevel + 1 when inserting as a child.
  3. Re-derive the level from the model's parent chain rather than computing it ad hoc.

Example fix

// before (previous item is root, level 0)
collection.InsertWithLevel(i, grandchild, 2);
// after (insert parent at level 1 first, then grandchild)
collection.InsertWithLevel(i, parent, 1);
collection.InsertWithLevel(i + 1, grandchild, 2);
Defensive patterns

Strategy: validation

Validate before calling

int prev = index > 0 ? collection.GetLevel(index - 1) : -1;
if (level > prev + 1) level = prev + 1;
collection.InsertWithLevel(index, item, level);

Prevention

When it happens

Trigger: 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).

Common situations: Inserting a grandchild directly after a root without first inserting the parent, or miscomputing the level after a drag-and-drop reorder.

Related errors


AI-assisted analysis of MaterialDesignInXAML/MaterialDesignInXamlToolkit@98edec3a0b (2026-08-13). Data as JSON: /api/errors/f255479fda0628fb. Report an issue: GitHub.