MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · ArgumentOutOfRangeException

index

Error message

index

What it means

GetParent walks backward from `index` to find the nearest ancestor at level-1. It validates that index is within [0, Count) and throws ArgumentOutOfRangeException(nameof(index)) otherwise.

Source

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

            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)
            {
                return this[i];
            }
        }
        return null;
    }

    public IEnumerable<int> GetDirectChildrenIndexes(int index)
    {
        if (index < 0 || index >= Count) throw new ArgumentOutOfRangeException(nameof(index));

        return GetDirectChildrenIndexesImplementation(index);

View on GitHub (pinned to 98edec3a0b)

Solutions

  1. Bounds-check the index: `if (index < 0 || index >= collection.Count) ...` before calling.
  2. Refresh indexes after any Add/Remove/Reset on the collection or its source.
  3. Use the index returned by collection.IndexOf rather than the source list position.

Example fix

// before
var parent = collection.GetParent(i);
// after
if (i >= 0 && i < collection.Count)
    var parent = collection.GetParent(i);
Defensive patterns

Strategy: validation

Validate before calling

if (index < 0 || index >= collection.Count)
    throw new ArgumentOutOfRangeException(nameof(index));
var parent = collection.GetParent(index);

Type guard

index >= 0 && index < collection.Count

Prevention

When it happens

Trigger: Calling GetParent(-1), GetParent(Count), or any index outside [0, Count).

Common situations: Using a stale index after a Reset/Remove without re-querying Count; off-by-one loop bounds; confusing the wrapped source-list index with the collection index.

Related errors


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