Fix SelectionChanged not raised on collection Reset#20942
Open
NathanDrake2406 wants to merge 1 commit intoAvaloniaUI:masterfrom
Open
Fix SelectionChanged not raised on collection Reset#20942NathanDrake2406 wants to merge 1 commit intoAvaloniaUI:masterfrom
NathanDrake2406 wants to merge 1 commit intoAvaloniaUI:masterfrom
Conversation
…deselected When a collection bound to a SelectingItemsControl (e.g. ListBox) was cleared via NotifyCollectionChangedAction.Reset, the SelectionChanged event was not raised despite the selection being lost. The root cause was in InternalSelectionModel.OnSourceReset: the base SelectionModel.OnSourceReset() directly reset _selectedIndex to -1 before any Operation could capture the old selection state. The subsequent SyncFromSelectedItems created an Operation that saw no change (old and new both -1), so CommitOperation never fired SelectionChanged. The fix snapshots _writableSelectedItems before sync, diffs against the post-sync state to find items that were actually lost (not merely re-selected at a new index after reorder), and injects them as DeselectedItems on the pending Operation — following the same pattern used by OnSelectionRemoved for individual item removals. Fixes AvaloniaUI#20897
|
You can test this PR using the following package version. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SelectionChangedevent is now raised when a bound collection is cleared (or reset) and the control had a selectionInternalSelectionModel.OnSourceResetcalledSyncFromSelectedItems()which created anOperationafterSelectionModel.OnSourceReset()had already reset_selectedIndexto-1— the Operation saw no change and never firedSelectionChanged_writableSelectedItemsbefore sync, diff against post-sync state to find actually-lost items, inject asDeselectedItemson the pending Operation (same pattern asOnSelectionRemoved)Test plan
SelectionChangedwith deselected item inRemovedItemsSelectionChangedwith all deselected itemsFixes #20897