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);
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With