Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SelectionModel: Fix for missing SelectionChanged event raise #2359

Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions dev/Repeater/APITests/SelectionModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,80 @@ void ThrowIfRaisedSelectionChanged(SelectionModel sender, SelectionModelSelectio
}
}

[TestMethod]
public void ValidateSelectionModeChangeFromMultipleToSingle()
{
RunOnUIThread.Execute(() =>
{
SelectionModel selectionModel = new SelectionModel();
selectionModel.Source = Enumerable.Range(0, 10).ToList();

// First test: switching from multiple to single selection mode with just one selected item
selectionModel.Select(4);

selectionModel.SingleSelect = true;

// Verify that the item at index 4 is still selected
Verify.IsTrue(selectionModel.SelectedIndex.CompareTo(Path(4)) == 0, "Item at index 4 should have still been selected");

// Second test: this time switching from multiple to single selection mode with more than one selected item
selectionModel.SingleSelect = false;
selectionModel.Select(5);
selectionModel.Select(6);

// Now switch to single selection mode
selectionModel.SingleSelect = true;

// Verify that
// - only one item is currently selected
// - the currently selected item is the item with the lowest index in the Multiple selection list
Verify.AreEqual(1, selectionModel.SelectedIndices.Count,
"Exactly one item should have been selected now after we switched from Multiple to Single selection mode");
Verify.IsTrue(selectionModel.SelectedIndices[0].CompareTo(selectionModel.SelectedIndex) == 0,
"SelectedIndex and SelectedIndices should have been identical");
Verify.IsTrue(selectionModel.SelectedIndex.CompareTo(Path(4)) == 0, "The currently selected item should have been the first item in the Multiple selection list");
});
}

[TestMethod]
public void ValidateSelectionChangedEventOnSwitchFromMultipleSelectionToSingleSelection()
Felix-Dev marked this conversation as resolved.
Show resolved Hide resolved
{
RunOnUIThread.Execute(() =>
{
SelectionModel selectionModel = new SelectionModel();
selectionModel.Source = Enumerable.Range(0, 10).ToList();

// First test: switching from multiple to single selection mode with just one selected item
selectionModel.Select(4);

int selectionChangedFiredCount = 0;
selectionModel.SelectionChanged += IncreaseCountIfRaisedSelectionChanged;

// Now switch to single selection mode
selectionModel.SingleSelect = true;

// Verify that no SelectionChanged event was raised
Verify.AreEqual(0, selectionChangedFiredCount, "SelectionChanged event should have not been raised as only one item was selected");

// Second test: this time switching from multiple to single selection mode with more than one selected item
selectionModel.SelectionChanged -= IncreaseCountIfRaisedSelectionChanged;
selectionModel.SingleSelect = false;
selectionModel.Select(5);
selectionModel.SelectionChanged += IncreaseCountIfRaisedSelectionChanged;

// Now switch to single selection mode
selectionModel.SingleSelect = true;

// Verify that the SelectionChanged event was raised
Verify.AreEqual(1, selectionChangedFiredCount, "SelectionChanged event should have been raised as the selection changed");

void IncreaseCountIfRaisedSelectionChanged(SelectionModel sender, SelectionModelSelectionChangedEventArgs args)
{
selectionChangedFiredCount++;
}
});
}

private void Select(SelectionModel manager, int index, bool select)
{
Log.Comment((select ? "Selecting " : "DeSelecting ") + index);
Expand Down
10 changes: 6 additions & 4 deletions dev/Repeater/SelectionModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,17 @@ void SelectionModel::SingleSelect(bool value)
{
m_singleSelect = value;
auto selectedIndices = SelectedIndices();
if (value && selectedIndices && selectedIndices.Size() > 0)

// Only update selection and raise SelectionChanged event when:
// - we switch from SelectionMode::Multiple to SelectionMode::Single and
// - more than one item was selected at the time of the switch
if (value && selectedIndices && selectedIndices.Size() > 1)
{
// We want to be single select, so make sure there is only
// one selected item.
auto firstSelectionIndexPath = selectedIndices.GetAt(0);
ClearSelection(true /* resetAnchor */, false /*raiseSelectionChanged */);
SelectWithPathImpl(firstSelectionIndexPath, true /* select */, false /* raiseSelectionChanged */);
// Setting SelectedIndex will raise SelectionChanged event.
SelectedIndex(firstSelectionIndexPath);
SelectWithPathImpl(firstSelectionIndexPath, true /* select */, true /* raiseSelectionChanged */);
}

RaisePropertyChanged(L"SingleSelect");
Expand Down