stride3d/stride · error · InvalidOperationException

Cannot call MoveItem on a SortedObservableCollection

Error message

Cannot call MoveItem on a SortedObservableCollection

What it means

SortedObservableCollection keeps itself sorted on every insert and therefore forbids reordering. MoveItem is overridden to always throw InvalidOperationException; the base ObservableCollection.MoveItem must never be reached directly. Use ObservableCollectionMoveItem or removal/re-insertion through the public API instead.

Solutions

  1. Remove any direct MoveItem calls on SortedObservableCollection
  2. If reordering is needed, remove and re-insert the item so the sort comparer repositions it
  3. Use a plain ObservableCollection if manual ordering is a requirement

Example fix

// before
sortedCollection.MoveItem(2, 5);
// after
var item = sortedCollection[2];
sortedCollection.Remove(item);
sortedCollection.Insert(5, item); // position may be adjusted by comparer
Defensive patterns

Strategy: type-guard

Validate before calling

if (collection is SortedObservableCollection<T>) throw new NotSupportedException("Reordering is not supported; remove & re-add instead.");

Type guard

bool CanReorder(object c) => c is not SortedObservableCollection<T>;

Try / catch

try { collection.MoveItem(oldIndex, newIndex); } catch (InvalidOperationException) { /* fall back to Remove+Insert */ }

Prevention

When it happens

Trigger: Calling MoveItem(oldIndex, newIndex) on a SortedObservableCollection instance, typically from collection-view drag-and-drop reordering or code that treats it as a plain ObservableCollection.

Common situations: Wiring a DataGrid or ListView drag-drop reorder handler to a sorted collection; refactoring code that used ObservableCollection to SortedObservableCollection without changing reorder logic.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/10c20b82d908d294. Report an issue: GitHub.

Appendix: source

Thrown at sources/presentation/Stride.Core.Presentation/Collections/SortedObservableCollection.cs:160

                imax = imid - 1;
            else
                return imid;
        }

        return returnInsertIndex ? imin : -1;
    }

    /// <inheritdoc/>
    protected override void InsertItem(int index, T item)
    {
        int i = GetIndex(item, true);
        base.InsertItem(i, item);
    }

    /// <inheritdoc/>
    protected override void MoveItem(int oldIndex, int newIndex)
    {
        throw new InvalidOperationException("Cannot call MoveItem on a SortedObservableCollection");
    }

    protected void ObservableCollectionMoveItem(int oldIndex, int newIndex)
    {
        base.MoveItem(oldIndex, newIndex);
    }

    /// <inheritdoc/>
    protected override void SetItem(int index, T item)
    {
        throw new InvalidOperationException("Cannot call SetItem on a SortedObservableCollection");
    }
}

View on GitHub (pinned to 96fad776d2)