locked
Listview selectedindex always gives -1

    Question

  • Hi everybody, I have a simple listview bonded with an observable collection to show some location results. Everything works fine, I can see my results in the lust view but when I try to use the  selectedindex of the listview I get -1. Here is the code

    <ListView x:Name="LocationsVL" ItemsSource="{Binding LocationsObs}">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock>
                                            <Run Text="{Binding Name}"/>
                                            <Run Text=","/>
                                            <Run Text="{Binding Address.CountryRegionIso2}"/>
                                        </TextBlock>
                                    </StackPanel>
                                </StackPanel>
                            </DataTemplate>
                        </ListView.ItemTemplate>                    
                    </ListView>

    and the code behind

    private void OnAddLocationClick(object sender, RoutedEventArgs e)
            {
                Location selectedLocation=new Location();            
                if (LocationsVL.SelectedIndex>=0) //gives always -1
                {
                    selectedLocation=LocationsVL.SelectedItem as Location;
                    vmSearchBing.savedLocations.Add(selectedLocation);
                }            
            }
    Thanks in advance.

    Sunday, October 12, 2014 5:46 AM

Answers

  • Hello,

    I believe you are trying to get the SelectedIndex of the ListView upon selecting an item in the list view.

    You should then use the SelectionChanged Event as below:

    XAML Code:

            <ListView x:Name="LocationsVL" SelectionChanged="LocationsVL_SelectionChanged">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock>
                                            <Run Text="{Binding Name}"/>
                                </TextBlock>
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
    C# Code:
            private void LocationsVL_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                Location selectedLocation = new Location();
                if (LocationsVL.SelectedIndex >= 0) //gives always -1
                {
                    selectedLocation = LocationsVL.SelectedItem as Location;
                    //vmSearchBing.savedLocations.Add(selectedLocation);
                }    
            }

    • Marked as answer by giannisdolon Sunday, October 12, 2014 9:10 AM
    Sunday, October 12, 2014 8:51 AM

All replies

  • Hello,

    I believe you are trying to get the SelectedIndex of the ListView upon selecting an item in the list view.

    You should then use the SelectionChanged Event as below:

    XAML Code:

            <ListView x:Name="LocationsVL" SelectionChanged="LocationsVL_SelectionChanged">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock>
                                            <Run Text="{Binding Name}"/>
                                </TextBlock>
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
    C# Code:
            private void LocationsVL_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                Location selectedLocation = new Location();
                if (LocationsVL.SelectedIndex >= 0) //gives always -1
                {
                    selectedLocation = LocationsVL.SelectedItem as Location;
                    //vmSearchBing.savedLocations.Add(selectedLocation);
                }    
            }

    • Marked as answer by giannisdolon Sunday, October 12, 2014 9:10 AM
    Sunday, October 12, 2014 8:51 AM
  • Thank you very much. It worked.
    Sunday, October 12, 2014 9:11 AM