Skip to content

Fix DataGrid not updating on collection Move operations#238

Open
xfortin-devolutions wants to merge 8 commits into
AvaloniaUI:masterfrom
xfortin-devolutions:fix-collection-move-not-updating-datagrid
Open

Fix DataGrid not updating on collection Move operations#238
xfortin-devolutions wants to merge 8 commits into
AvaloniaUI:masterfrom
xfortin-devolutions:fix-collection-move-not-updating-datagrid

Conversation

@xfortin-devolutions
Copy link
Copy Markdown

@xfortin-devolutions xfortin-devolutions commented May 12, 2026

Fix DataGrid not updating on collection Move operations

Summary

  • Add NotifyCollectionChangedAction.Move handling to DataGridCollectionView.ProcessCollectionChanged, which translates Move events from the source collection into Remove+Add events that the DataGrid already knows how to process
  • The insertion index is computed from the event args to account for AvaloniaList.MoveRange reporting NewStartingIndex in the original (pre-removal) index space
  • No-op moves (where the target index falls within the moved block) are detected and skipped
  • Move handling for grouped DataGrids and direct NotifyingDataSource_CollectionChanged is left as future work (marked with TODO comments)
  • Add a "Collection Mutations" tab to the sample app to exercise Add, Add Range, Remove, Move Up/Down, and Move In Place operations against a DataGrid backed by an AvaloniaList

Known limitations

  • Grouped DataGrid scenarios (CollectionViewGroup_CollectionChanged) do not handle Move yet

grokys added a commit that referenced this pull request May 14, 2026
Copy link
Copy Markdown
Member

@grokys grokys left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Having experience of this problem in the past, I knew where to look: selection ;) I've created a branch with a number of tests, some of them failing, many of them around selection.

Other than the bugs hightlighted in this branch, one of the things to decide is:

With this PR, when a selected item is moved, the selection remains at the same index and a different item gets selected. Is this OK? At first I thought that this was totally acceptable, but then I noticed that you're working around this in the sample, which suggests that it isn't ideal behavior?

There are tests for this in the branch but it might warrent discussion as to what the desired behavior is.

Other than that, I would ask for more tests in general for this new functionality. I know that this control is really lacking unit tests, but it would be nice to cover at least new functionality.

{
var selected = dgMutations.SelectedItems.Cast<Person>().ToList();
mutationList.MoveRange(firstIndex, count, firstIndex - 1);
dgMutations.SelectedItems.Clear();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the workaround that I'm referring to.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would agree that this is not ideal. I'd say, on a user point of view, I would expect my selection to remain the same after a move. That is, all the items (not indices) that have been moved, if they were selected, should remain selected.

In fact, most of the time, a move would imply the selection anyway. I can look into handling this correctly at the control level.

As for more tests, you mean, additionally to the one you've added?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah the tests I added were just around the areas where my spidey senses told me there might be issues. Would be good to get more tests of the basic move behavior too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants