freddy
freddy

Reputation: 394

WP7 listpicker selectionchanged selectedindex return -1

I have i little and simple problem. I think.

I have to Listpickers where the second depends on the selection from the first. I've thought that i could easily be done with use of selectionchanged on the first Listpicker and then get the selected index.

<toolkit:ListPicker ExpansionMode="FullScreenOnly" Grid.Row="0" Name="customers" FullModeHeader="Kunder" Margin="10,50,10,10" Width="350" HorizontalAlignment="Left" SelectionChanged="customers_SelectionChanged">
      <toolkit:ListPicker.ItemTemplate>
              <DataTemplate>
                     <StackPanel Orientation="Horizontal">
                          <TextBlock Text="{Binding Title}" Margin="12,0,0,0"></TextBlock>
                     </StackPanel>
              </DataTemplate>
      </toolkit:ListPicker.ItemTemplate>
      <toolkit:ListPicker.FullModeItemTemplate>
             <DataTemplate>
                    <StackPanel Orientation="Horizontal" Margin="16,20,0,20">
                         <TextBlock Text="{Binding Title}" Margin="16,0,0,0" FontSize="30" FontFamily="{StaticResource PhoneFontFamilyLight}"></TextBlock>
                    </StackPanel>
             </DataTemplate>
      </toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>

And the code for selectionchanged:

private void customers_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        int selindex = customers.SelectedIndex;

        MessageBox.Show("index : " + selindex);
        Guid costumerid = customers[selindex].id;
        Loadprojects();
    }

My problem is that selindex always equals -1 and then i get a out of range exception. What is the best way to solve this?

Upvotes: 0

Views: 941

Answers (1)

peromed
peromed

Reputation: 58

private void customers_SelectionChanged(object sender, SelectionChangedEventArgs e)
{

    int selindex = customers.SelectedIndex;
    if (selindex==-1) return;
    MessageBox.Show("index : " + selindex);
    Guid costumerid = customers[selindex].id;
    Loadprojects();
}

Upvotes: 1

Related Questions