3

I am running into a very odd problem in c# and I just wanted to know what is causing this. I have my theories, but not entirely sure and just want to see if it can be reproduced.

Standard Pivot Page in wp7 silverlight 4.

<Pivot>
  <PivotItem>
     <Listbox Width="400" Height="500" x:Name="box" SelectionChanged="myhandle">

        <ListBoxItem x:Name="item1">
           <TextBlock Height="40" Width="200" Text="hi everyone!"/>
        </ListBoxItem>

        <ListBoxItem x:Name="item2">
           <TextBlock Height="40" Width="200" Text="No Wai"/>
        </ListBoxItem>

        <ListBoxItem x:Name="item3">
           <TextBlock Height="40" Width="200" Text="Ya Rly!"/>
        </ListBoxItem>

     </Listbox>
  </PivotItem>
</Pivot>

In my C#, I have the following:

  private void myhandle(object sender, SelectionChangedEventArgs args)
  {
    var selection ="";
    selection = (sender as Listbox).SelectedIndex.ToString();
    box.SelectedIndex = -1;
  }

Here is the problem: Whenever I click on one of the three listboxitems, the myhandle code makes selection equal to the proper SelectedIndex, but then it hits the box.SelectedIndex =-1; line and then refires the myhandle function. In doing so, selection is now -1.

I have no idea why it is going back up the stack. This shouldn't be a recursive function.

My goal is to select the item, but then have the SelectedIndex back to -1 so that the person is able to select the item once again if need be, instead of changing to another item and back.

Sure there is an easy fix of throwing a switch function and checking to see if it's -1 already, but that doesn't solve the problem of the recursion.

Thanks for the time.

Shaun Doty
  • 125
  • 1
  • 9

3 Answers3

9

Everytime the selection is changed, the SelectionChanged event will fire. This includes clearing the selection, which includes setting SelectedIndex = -1 and even if you are already in a SelectionChanged handler.

You can do something like this:

private bool inMyHandle = false;
private void myhandle(object sender, SelectionChangedEventArgs args)
{
    if (!this.isMyHandle) {
        this.isMyHandle = true;
        try {
            var selection ="";
            selection = (sender as Listbox).SelectedIndex.ToString();
            box.SelectedIndex = -1;
        }
        finally {
            this.isMyHandle = false;
        }
    }
}
CodeNaked
  • 40,753
  • 6
  • 122
  • 148
6

The standard MS samples already have this in a standard Listbox selected item event.

Simply use the following in the event handler code:

    private void ListBox_SelectionChanged(object sender,System.Windows.Controls.SelectionChangedEventArgs e)
{
    // If selected index is -1 (no selection) do nothing
    if (ListBox.SelectedIndex == -1)
        return;

    //Do Something

    // Reset selected index to -1 (no selection)
    ListBox.SelectedIndex = -1;
}

No need to have any boolean handlers, the function just nothing if "-1" is the current index. All this is to compensate for the way that the standard listbox opperates.

If you use MVVM and bind to the "Selecteditem" / "SelectedIndex" properties you have to keep the same thing in mind.

Darkside
  • 1,722
  • 14
  • 19
4

You can check args.AddedItems.Count too:

private void myhandle(object sender, SelectionChangedEventArgs args) 
{
   if (args.AddedItems.Count > 0)
   {
       ....
       box.SelectedIndex = -1;
   }
}
johniecco
  • 53
  • 5