MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · ArgumentOutOfRangeException

Item level must not be negative

Error message

Item level must not be negative

What it means

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.

Source

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

                // We've have passed the provided index, which means we've found a non-prior root parentLevel item; bail out.
                break;
            }
        }
        return priorNonRootLevelItems;
    }

    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;

View on GitHub (pinned to 98edec3a0b)

Solutions

  1. Clamp or validate: use Math.Max(0, computedLevel) before calling.
  2. Use level 0 for root items; children are parentLevel + 1.
  3. Add an explicit `if (level < 0) throw;` guard at the call site during debugging to catch the source of the bad value.

Example fix

// before
collection.InsertWithLevel(i, child, parentLevel - 1);
// after
collection.InsertWithLevel(i, child, parentLevel + 1);
Defensive patterns

Strategy: validation

Validate before calling

if (level < 0) level = 0; // or throw at the call site
collection.InsertWithLevel(index, item, level);

Prevention

When it happens

Trigger: Calling collection.InsertWithLevel(index, item, level) with level < 0.

Common situations: 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.

Related errors


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