locked
How to get index of focused item in ListView? RRS feed

  • Question

  • Is it possible to get index of current focused item in ListView?

    Thanks for your reply.

    Thursday, November 8, 2012 6:06 PM

Answers

  • Hi gaurav77,

    See if the following code helps you:

    <ListView x:Name="focusListView">
                   <ListViewItem GotFocus="ListViewItem_GotFocus_1">
                        <TextBlock Text="Item1"/>
                    </ListViewItem>
                    <ListViewItem GotFocus="ListViewItem_GotFocus_1">
                        <TextBlock Text="Item2"/>
                    </ListViewItem>
    </ListView>
    private void ListViewItem_GotFocus_1(object sender, RoutedEventArgs e)
            {
                ListViewItem lv = sender as ListViewItem;
                var lvi= focusListView.ItemContainerGenerator.ItemFromContainer(lv);
                int index= focusListView.Items.IndexOf(lvi);
            }
    Thanks.

    Vicky Song [MSFT]
    MSDN Community Support | Feedback to us

    Friday, November 9, 2012 5:51 AM
    Moderator