3

In my Windows Phone 7 application I have a number of list views. Each listViewItem has a click event (By setting 'selectionChanged' attribute to the listBox in my xaml). Now a very peculiar thing happens:

When I click on an item in the listbox the first time everything goes well, in this case the user gets taken to another screen. When I go back from that screen to the listbox, I select the very same listboxitem but this time the event doesn't register, nothing happens...

I then first have to tap on another item, let that ones even fire, then only can I tap on the first item. So in other words, I can't fire a click event for a listItem twice in a row. I'm thinking it's because the event handler on the listbox says 'onSelectionChanged', if you select the same item the selection hasn't technically changed.

So what other eventHandling attribute can I use on my listbox to register selection events on it's items?

Thanks for any help!

2 Answers 2

3

AFAIK, Theres is no such event. So, the work around is,

In the OnNavigatedTo event handler of the first page, set the SelectedIndex to -1

YourListBox.SelectedIndex = -1;

And while doing so, make one small modification to your Selection_Changed handler

void Selection_Changed(...)
{
   if(YourListBox.SelectedIndex == -1)
    return;
   //rest of your code
}
3

In your case, SelectedItem in the ListBox is set for the first time. The second time you tap on the same item, technically its not a SelectionChanged event, hence its not firing.

Clearing the SelectedItem at the end of SelectionChanged event would do the trick.
Below is a code snippet that could be helpful,

private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    //
    // do your stuff here
    //

    //reset the selection of the sender (ListBox)
    (sender as ListBox).SelectedItem = null;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.