Avalonia icon indicating copy to clipboard operation
Avalonia copied to clipboard

DataGrid does not update for some ObservableCollection operations

Open alexandrehtrb opened this issue 7 months ago • 4 comments

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

alexandrehtrb avatar Nov 15 '23 16:11 alexandrehtrb

Please attach or link the sample shown. I believe it comes from the sorting the datagrid does provide

timunie avatar Nov 15 '23 18:11 timunie

DataGrid collection view doesn't handle Move operation: https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Controls.DataGrid/Collections/DataGridCollectionView.cs#L3414C32-L3454

maxkatz6 avatar Nov 15 '23 20:11 maxkatz6

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

alexandrehtrb avatar Nov 16 '23 00:11 alexandrehtrb

ref https://github.com/AvaloniaUI/Avalonia/issues/3606

ltetak avatar May 02 '24 08:05 ltetak