Search code examples
c#visual-studio-2010listboxwindows-phone-7.1selectedindex

How to select a ListBoxItem which is already selected OR to highlight previous selected ListBoxItem after setting SelectedIndex to -1?


I'm programming a simple listbox in windows phone 7. The listbox has some items and when I click any of items the app navigates to a new page.

From beginning to here, everything is good.

But I want end-user can select the item again which causes navigate to the next page again. But listbox as it is, doesn't allow me to select an already selected item again.

I tried to do this for allowing to select an item again.

private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if(ListBox.SelectedIndex != -1 )
    {
       NavigationService.Navigate(uri);
       ListBox.SelectedIndex = -1; 
    }
}

I have alreadhy edited the ListBoxItemTemplate to highlight selected item, but when I use above code, I cannot highlight the selected item, because it changes SelectedIndex too fast.

So how can I allow user to select a selected item or how can I highlight previous selected item. Any suggestions or tips?

EDIT: When using a normal ListBox , I can simply use :

listboxitem1.Background = new SolidColorBrush(Color.Blue);`

But when I edit template of listbox item this does throw exception, so I cannot still do this.


Solution

  • Well, you can edit the template of ListBoxItem and add a grid, fully front of listbox item. And bind the visibility property of this grid.

    After this, you can change of the visibility according to selected index. This approach should work well if you're using a navigation with listbox.

    Hope it works!