Kenny Witham Kenny Witham - 8 days ago 6
C# Question

Index of ListViewItem Tapped in Tapped Event

So, I am designing a Universal Windows app which uses a ListView with data bound to it which i create programatically. The XAML for my page:

<ListView x:Name="lvEpisodeListSource" Margin="10,170,10,10" ItemsSource="{Binding Source={StaticResource EpisodeListSource}}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:Episode">
<ListViewItem Background="CadetBlue" IsDoubleTapEnabled="False" IsHoldingEnabled="False" Tapped="ListViewItem_Tapped" IsRightTapEnabled="False" HorizontalAlignment="Stretch">
<TextBlock Name="AlbumBlock" Foreground="Black" FontWeight="Normal" FontSize="15" Margin="5,0,0,0"
Text="{x:Bind Name}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
</ListViewItem>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
</ListView>


The data binds correctly and that all works fine. I am trying to then save the most recently clicked ListViewItem to LocalApplicationData. Currently, I am trying to do this by setting the Local App Data in the tapped event for the Items.

private void ListViewItem_Tapped(object sender, TappedRoutedEventArgs e)
{
myData data = ((ListViewItem)sender).DataContext as myData;
var clickedNumber = lvDataListSource.Items.IndexOf(((ListViewItem)sender));
}


This is currently not working and the
clickedNumber
always returns -1 no matter which I click. Is there a way to get the index of the item which called the tapped event or is there a better way to do want I want to accomplish?

Thanks!

Answer

Getting index or data corresponding to whole ListView (Parent Context) Instead of ListViewItem (Child Context) is always better.

Change your XAML to below.

<ListView x:Name="lvEpisodeListSource" Margin="10,170,10,10" ItemsSource="{Binding Source={StaticResource EpisodeListSource}}" SelectionChanged="ListView_SelectionChanged>
    <ListView.ItemTemplate>
        <DataTemplate x:DataType="local:Episode">
                <TextBlock Name="AlbumBlock" Foreground="Black" FontWeight="Normal" FontSize="15" Margin="5,0,0,0"
                                Text="{x:Bind Name}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
        </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

and your Selection Changed event would be

private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ListView view = (ListView)sender;
    //Get Index of Selected Item
    var index = view.SelectedIndex;
    //Get Selected Item
    var selectedItem = view.SelectedItem;
}

I would suggest always to refer parent resource and try to drill down to child unless you have a child click event which gives direct resource to get the parent.