Avalonia
Avalonia copied to clipboard
DataGrid does not update for some ObservableCollection operations
I am testing Avalonia.Xaml.Behaviors DragAndDropSample project and detected that some operations on ObservableCollection do not cause the DataGrid to be updated.
1) ObservableCollection.Move()
I refactored the MoveItem method into this:
protected void MoveItem<T>(ObservableCollection<T> items, int sourceIndex, int targetIndex) =>
items.Move(sourceIndex, targetIndex);
For ListBox, the drag-and-drop movement is still OK, but for DataGrid, it stops working.
https://github.com/AvaloniaUI/Avalonia/assets/27026741/4c19d1db-61fd-45ee-b5b9-90fef5863074
2) ObservableCollection.RemoveAt + ObservableCollection.Insert
Refactored the SwapItem method because it was flaky, even with ListBox; became like this:
protected void SwapItem<T>(IList<T> items, int sourceIndex, int targetIndex)
{
var item1 = items[sourceIndex];
var item2 = items[targetIndex];
items.RemoveAt(sourceIndex);
items.Insert(sourceIndex, item2);
items.RemoveAt(targetIndex);
items.Insert(targetIndex, item1);
}
Then, testing the swap action (Alt+ drag item), it works for ListBox, but weird result for DataGrid:
https://github.com/AvaloniaUI/Avalonia/assets/27026741/5dd94a4b-2243-4394-8e09-2c7efc16ce0d
Please attach or link the sample shown. I believe it comes from the sorting the datagrid does provide
DataGrid collection view doesn't handle Move operation: https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Controls.DataGrid/Collections/DataGridCollectionView.cs#L3414C32-L3454
Please attach or link the sample shown. I believe it comes from the sorting the datagrid does provide
https://github.com/AvaloniaUI/Avalonia.Xaml.Behaviors/pull/143
Doesn't contain the changes in MoveItem and SwapItem that I mentioned above
ref https://github.com/AvaloniaUI/Avalonia/issues/3606