How do I clear the selection in other ListView properly? For example I have 4 list views stacked, and if I have a selection in listView1, then I select a new item in listView2, I want the selection in listView1 to be gone and the item in listview2 is selected properly. Note: the list view is single selection.
<StackPanel>
<ListView x:Name="listView1" />
<ListView x:Name="listView2" />
<ListView x:Name="listView3" />
<ListView x:Name="listView4" />
</StackPanel>
I first tried:
<StackPanel>
<ListView x:Name="listView1" SelectionChanged="selectionchanged1" />
<ListView x:Name="listView2" SelectionChanged="selectionchanged2" />
<ListView x:Name="listView3" SelectionChanged="selectionchanged3" />
<ListView x:Name="listView4" SelectionChanged="selectionchanged4" />
</StackPanel>
code behind:
private void selectionChanged1(object sender, SelectionChangedEventArgs e)
{
listView2.SelectedItem = null;
listView3.SelectedItem = null;
listView4.SelectedItem = null;
}
private void selectionChanged2(object sender, SelectionChangedEventArgs e)
{
listView1.SelectedItem = null;
listView3.SelectedItem = null;
listView4.SelectedItem = null;
}
...
The problem I am having is that after having a selection in listView1 and click an item in listView2, the selection in listView1 is cleared, but the item in listView2 is not selected.
The reason is because there are (at least) 2 events fired:
- selectionchanged2 fired, claring selections in listview 1,3,4
- because I am clearing selection in listview1, selectionchanged1 is also fired, which clears selections in listview 2,3,4
- I think selectionchanged2 is fired again.
- Therefore I ended up with no selection in any list.
Do you have better suggestion?