Deselect listbox item in c#

16,294

You can call UnselectAll() method

listBox1.UnselectAll();
Share:
16,294
bytestorm
Author by

bytestorm

I do have an imaginary friend...

Updated on June 04, 2022

Comments

  • bytestorm
    bytestorm almost 2 years

    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.