I have a question about Mvvmlight binding Listbox SelectedIndex.
The full source code can be downloaded here.
Here is my precondition:
[TestModel]
string Title;
string Description;
[TestViewModel]
ObservableCollection<TestModel> TestList;
[xaml binding]
<ListBox ItemSource="{Binding TestList}"
SelectedIndex="{Binding SelectedIndex Mode=TwoWay}">
.....
<i:EventTrigger EventName="SelectionChanged">
.....
</i:EventTrigger>.....
Here is my OnSelectionChanged code:
private void OnSelectionChanged(TestModel test)
{
int index = SelectedIndex;
Debug.WriteLine("[SelectionChanged] +++, index={0}", index);
// If selected index is -1 (no selection) do nothing
if (-1 == SelectedIndex)
return;
Debug.WriteLine("[SelectionChanged] selected item={0}", test.Title);
// Reset selected index to -1 (no selection)
SelectedIndex = -1;
Debug.WriteLine("[SelectionChanged] ---, index={0}", index);
}
I have a sample to have MainPage.xaml and TestPage.xaml.
- MainPage: This page has a button, click the button will navigate to TestPage
- TestPage: This page has a listbox and binding to ViewModel
When I run this sample, tap button to TestPage, and try to tap any item of Listbox, I can see the item has no focused color (because I reset SelectedIndex to -1).
Here comes the question, when I back to MainPage, then again tap button to TestPage, you will see tapping any item of Listbox will cause focused color on every item, it’s strange.
Hope anyone can help me to see if any problem on my sample.
Thanks.
I got a workaround from somebody below:
Change:
To:
It really works as what I expected.
Tap can allow user to tap repeated ListItem, also “reset SelectedIndex = -1” can make selected item without highlight color.