Commit b0877fe
authored
Fix SelectionChanged not raised on collection Reset (#20942)
* fix: raise SelectionChanged event on collection Reset when items are 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 #20897
* fix: use multiset diff to report lost duplicate selections on Reset
The Reset diff in InternalSelectionModel used a HashSet to detect
which previously-selected items were still present after sync. Selection
allows duplicates (same instance or equal items at multiple indices),
so set semantics collapsed duplicates into one entry and under-reported
deselections when only some occurrences were lost.
Track counts per item plus a null counter and decrement per match, so
RemovedItems reflects the actual number of lost selections.
Adds a duplicate-items Reset test covering the regression.
* fix: raise SelectionChanged for reset-lost selection
ListBox and other SelectingItemsControl callers did not receive SelectionChanged when a Reset cleared the selected items. The selection model reports this path through LostSelection, but the control only used that callback for AlwaysSelected recovery.
Track the last selected items at the control boundary, capture that snapshot for Reset notifications, and raise the routed SelectionChanged event when LostSelection commits during that reset. This avoids diffing reset contents while preserving the removed-items payload for clear/reset-to-empty cases.
* fix: address review feedback on SelectionChanged Reset snapshot
- Replace per-change ToArray() snapshot with persistent List<object?> to
avoid allocations on every selection change (review: MrJul).
- Read snapshot in PreCollectionChanged instead of Selection.SelectedItems
because the source is already empty by the time Reset fires.
- Align LostSelection event-raising with SelectionChanged path: use
BuildEventRoute + HasHandlers guard to avoid allocating args when
no handlers are attached (review: copilot).
- Harden existing Reset tests to Assert.Single to catch double-fire.
* fix: consolidate SelectionChanged raising and defend against stale snapshot
- Extract RaiseSelectionChanged helper so both the normal
(SelectionChanged) and reset (LostSelection) paths share the same
BuildEventRoute/HasHandlers guard and SelectionChangedEventArgs
construction (review: copilot).
- Move _selectedItemsBeforeReset clear outside the conditional in
OnSelectionModelLostSelection so the field is always nulled after
LostSelection, preventing accidental reuse (review: copilot).
- Add comment documenting the snapshot lifecycle in
OnItemsViewPreCollectionChanged.
* perf: defer SelectionChangedEventArgs allocations until handlers are confirmed
SelectionChangedEventArgs materialized arrays via ToArray() at the call site
before checking whether the routed event had any registered handlers. This
allocated needlessly in the common case of no external subscribers.
The event items (IReadOnlyList<object?>) already implement IList via
ReadOnlySelectionListBase. RaiseSelectionChanged now accepts
IReadOnlyList<object?> and casts to IList inside the HasHandlers gate,
falling back to ToArray() only for non-IList enumerables.1 parent 498c183 commit b0877fe
2 files changed
Lines changed: 140 additions & 10 deletions
File tree
- src/Avalonia.Controls/Primitives
- tests/Avalonia.Controls.UnitTests/Primitives
Lines changed: 45 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
145 | 146 | | |
146 | 147 | | |
147 | 148 | | |
| 149 | + | |
| 150 | + | |
148 | 151 | | |
149 | 152 | | |
150 | 153 | | |
| |||
153 | 156 | | |
154 | 157 | | |
155 | 158 | | |
156 | | - | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
157 | 162 | | |
158 | 163 | | |
159 | 164 | | |
| |||
465 | 470 | | |
466 | 471 | | |
467 | 472 | | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
468 | 481 | | |
469 | 482 | | |
470 | 483 | | |
| |||
1013 | 1026 | | |
1014 | 1027 | | |
1015 | 1028 | | |
1016 | | - | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
1017 | 1032 | | |
1018 | | - | |
1019 | | - | |
1020 | | - | |
1021 | | - | |
1022 | | - | |
1023 | | - | |
1024 | | - | |
1025 | | - | |
| 1033 | + | |
1026 | 1034 | | |
1027 | 1035 | | |
1028 | 1036 | | |
| |||
1033 | 1041 | | |
1034 | 1042 | | |
1035 | 1043 | | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
1036 | 1051 | | |
1037 | 1052 | | |
1038 | 1053 | | |
1039 | 1054 | | |
1040 | 1055 | | |
1041 | 1056 | | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
| 1073 | + | |
| 1074 | + | |
1042 | 1075 | | |
1043 | 1076 | | |
1044 | 1077 | | |
| |||
1248 | 1281 | | |
1249 | 1282 | | |
1250 | 1283 | | |
| 1284 | + | |
| 1285 | + | |
1251 | 1286 | | |
1252 | 1287 | | |
1253 | 1288 | | |
| |||
Lines changed: 95 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
856 | 856 | | |
857 | 857 | | |
858 | 858 | | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
859 | 954 | | |
860 | 955 | | |
861 | 956 | | |
| |||
0 commit comments