Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the SelectedIndex of a LongListSelector Item

I have a WP8 DataBound app with an ItemViewModel bound to a LongListSelector.

Quite simply, when the user taps on an item in the list, I need to retrieve the index number of the item selected for future use. (0 is first in the list, 1 is second, etc.)

So, just as this might retrieve a certain property of the selected item:

string whateverProperty = (MyLongListSelector.SelectedItem as ItemViewModel).WhateverProperty;

I need something like this (obviously made up code):

int indexNumber = (MyLongListSelector.SelectedItem as ItemViewModel).GetSelectedIndex();

I think the SelectedIndex property is the thing I need but I can't figure out how retrieve it.

Thank you!

EDIT: SOLVED! The following gets me exactly what I was looking for:

 int selectedIndex = App.ViewModel.Items.IndexOf(MainLongListSelector.SelectedItem as ItemViewModel);
like image 265
user2373614 Avatar asked Nov 12 '22 05:11

user2373614


1 Answers

I had the same problem. You need to use the ItemSource to retrieve the index. It should match your data template index for index.

int selectedIndex = selector.ItemsSource.IndexOf(selector.SelectedItem as ItemViewModel);

selector references the LongListSelector object sender. Hope this helps!

like image 81
ZeroSilver Avatar answered Nov 15 '22 12:11

ZeroSilver