{"record":{"id":"10c20b82d908d294","repo":"stride3d/stride","slug":"cannot-call-moveitem-on-a-sortedobservablecollection","errorCode":null,"errorMessage":"Cannot call MoveItem on a SortedObservableCollection","messagePattern":"Cannot call MoveItem on a SortedObservableCollection","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/presentation/Stride.Core.Presentation/Collections/SortedObservableCollection.cs","lineNumber":160,"sourceCode":"                imax = imid - 1;\n            else\n                return imid;\n        }\n\n        return returnInsertIndex ? imin : -1;\n    }\n\n    /// <inheritdoc/>\n    protected override void InsertItem(int index, T item)\n    {\n        int i = GetIndex(item, true);\n        base.InsertItem(i, item);\n    }\n\n    /// <inheritdoc/>\n    protected override void MoveItem(int oldIndex, int newIndex)\n    {\n        throw new InvalidOperationException(\"Cannot call MoveItem on a SortedObservableCollection\");\n    }\n\n    protected void ObservableCollectionMoveItem(int oldIndex, int newIndex)\n    {\n        base.MoveItem(oldIndex, newIndex);\n    }\n\n    /// <inheritdoc/>\n    protected override void SetItem(int index, T item)\n    {\n        throw new InvalidOperationException(\"Cannot call SetItem on a SortedObservableCollection\");\n    }\n}\n","sourceCodeStart":142,"sourceCodeEnd":174,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/presentation/Stride.Core.Presentation/Collections/SortedObservableCollection.cs#L142-L174","documentation":"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.","triggerScenarios":"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.","commonSituations":"Wiring a DataGrid or ListView drag-drop reorder handler to a sorted collection; refactoring code that used ObservableCollection to SortedObservableCollection without changing reorder logic.","solutions":["Remove any direct MoveItem calls on SortedObservableCollection","If reordering is needed, remove and re-insert the item so the sort comparer repositions it","Use a plain ObservableCollection if manual ordering is a requirement"],"exampleFix":"// before\nsortedCollection.MoveItem(2, 5);\n// after\nvar item = sortedCollection[2];\nsortedCollection.Remove(item);\nsortedCollection.Insert(5, item); // position may be adjusted by comparer","handlingStrategy":"type-guard","validationCode":"if (collection is SortedObservableCollection<T>) throw new NotSupportedException(\"Reordering is not supported; remove & re-add instead.\");","typeGuard":"bool CanReorder(object c) => c is not SortedObservableCollection<T>;","tryCatchPattern":"try { collection.MoveItem(oldIndex, newIndex); } catch (InvalidOperationException) { /* fall back to Remove+Insert */ }","preventionTips":["Never call ObservableCollection.MoveItem-style APIs on sorted collections","Implement drag-drop reorder as Remove+Insert","Keep sorted collections immutable w.r.t. order in view code"],"tags":["csharp","collections","unsupported-operation"],"backgroundTag":"unsupported-operation","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}