3
votes

I am using listboxes in a windows phone application code written in c#.

<Grid>
<ListBox x:Name ="gsecList" ItemsSource="{Binding}" SelectionChanged="ShowGsecDetails">

Event Handler :

private void ShowGsecDetails(object sender, SelectionChangedEventArgs e)
{
    string indexCode = gsecList.SelectedIndex.ToString();
    NavigationService.Navigate(new Uri("/contactDetail.xaml?type=gsec&index="+indexCode, UriKind.Relative));
}

I am using the eventhandler listBox1.SelectionChanged to navigate to some other page depending on the selection made by the user. Now when I navigate back to the page again I see the listITem still selected. How can I deselect that item? I tried to use listBox1.SelectedIndex = -1. But that seemed to call up the selectionChanged event handler.

3
you could just handle when the SelectedIndex == -1 and then not continue... - Secret Squirrel
@SecretSquirrel thanks. I am going to give it a try. - bytestorm
@SecretSquirrel thank you.. you solved my problem. - bytestorm
Thanks but I think you might want to look at "Anirudh" answer as that may be a cleaner way of deselecting items. - Secret Squirrel

3 Answers

7
votes

You can either do ListBox1.UnselectAll() or ListBox1.SelectedIndex = -1

But in the second case you have to put a breakpoint in the SelectionChanged event handler to see if index is -1 (in that case don't execute the code). Hope this helps

5
votes

You can call UnselectAll() method

listBox1.UnselectAll();
0
votes

Override OnNavigatedTo and set null as selected item.