Skip to content

Commit 6b1e4c7

Browse files
committed
Also handle deselects
1 parent 24afb0f commit 6b1e4c7

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

dev/Repeater/APITests/SelectionModelTests.cs

+53
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,59 @@ void SelectionModel_SelectionChanged(SelectionModel sender, SelectionModelSelect
10391039
}
10401040
}
10411041

1042+
[TestMethod]
1043+
public void AlreadyDeselectedDoesNotRaiseEvent()
1044+
{
1045+
var testName = "Deselect(int32 index), single select";
1046+
1047+
RunOnUIThread.Execute(() =>
1048+
{
1049+
var list = Enumerable.Range(0, 10).ToList();
1050+
1051+
var selectionModel = new SelectionModel() {
1052+
Source = list,
1053+
SingleSelect = true
1054+
};
1055+
1056+
// Single select index
1057+
selectionModel.SelectionChanged += SelectionModel_SelectionChanged;
1058+
selectionModel.Deselect(0);
1059+
1060+
selectionModel = new SelectionModel() {
1061+
Source = list,
1062+
SingleSelect = true
1063+
};
1064+
// Single select indexpath
1065+
testName = "DeselectAt(IndexPath index), single select";
1066+
selectionModel.SelectionChanged += SelectionModel_SelectionChanged;
1067+
selectionModel.DeselectAt(IndexPath.CreateFrom(1));
1068+
1069+
// multi select index
1070+
selectionModel = new SelectionModel() {
1071+
Source = list
1072+
};
1073+
testName = "Deselect(int32 index), multiselect";
1074+
selectionModel.SelectionChanged += SelectionModel_SelectionChanged;
1075+
selectionModel.Deselect(1);
1076+
selectionModel.Deselect(2);
1077+
1078+
selectionModel = new SelectionModel() {
1079+
Source = list
1080+
};
1081+
1082+
// multi select indexpath
1083+
testName = "DeselectAt(IndexPath index), multiselect";
1084+
selectionModel.SelectionChanged += SelectionModel_SelectionChanged;
1085+
selectionModel.DeselectAt(IndexPath.CreateFrom(1));
1086+
selectionModel.DeselectAt(IndexPath.CreateFrom(2));
1087+
});
1088+
1089+
void SelectionModel_SelectionChanged(SelectionModel sender, SelectionModelSelectionChangedEventArgs args)
1090+
{
1091+
throw new Exception("SelectionChangedEvent was raised, but shouldn't have been raised as selection did not change. Tested method: " + testName);
1092+
}
1093+
}
1094+
10421095
private void Select(SelectionModel manager, int index, bool select)
10431096
{
10441097
Log.Comment((select ? "Selecting " : "DeSelecting ") + index);

dev/Repeater/SelectionModel.cpp

+13-2
Original file line numberDiff line numberDiff line change
@@ -633,13 +633,19 @@ void SelectionModel::SelectWithPathImpl(const winrt::IndexPath& index, bool sele
633633
newSelection = false;
634634
}
635635
}
636+
else
637+
{
638+
// If we are in single select and selectedIndex is null, deselecting is not a new change.
639+
// Selecting something is a new change, so set flag to appropriate value here.
640+
newSelection = select;
641+
}
636642
}
637643

638644
// Selection is actually different from previous one, so update.
639645
if (newSelection)
640646
{
641647
// If we unselect something, raise event any way, otherwise changedSelection is false
642-
bool changedSelection = !select;
648+
bool changedSelection = false;
643649

644650
if (m_singleSelect)
645651
{
@@ -654,7 +660,7 @@ void SelectionModel::SelectWithPathImpl(const winrt::IndexPath& index, bool sele
654660
{
655661
if (depth == path.GetSize() - 1)
656662
{
657-
if (!currentNode->IsSelected(childIndex))
663+
if (currentNode->IsSelected(childIndex) != select)
658664
{
659665
// Node has different value then we want to set, so lets update!
660666
changedSelection = true;
@@ -669,6 +675,11 @@ void SelectionModel::SelectWithPathImpl(const winrt::IndexPath& index, bool sele
669675
AnchorIndex(index);
670676
}
671677

678+
// The walk tree operation can change the indices, and the next time it get's read,
679+
// we would throw an exception. That's what we are preventing with next two lines
680+
m_selectedIndicesCached = nullptr;
681+
m_selectedItemsCached = nullptr;
682+
672683
if (raiseSelectionChanged && changedSelection)
673684
{
674685
OnSelectionChanged();

0 commit comments

Comments
 (0)