获取一个LongListSelector Item的SelectedIndex [英] Getting the SelectedIndex of a LongListSelector Item
问题描述
我有一个WP8 DataBound应用程序,一个 ItemViewModel
绑定到一个 LongListSelector
。
I have a WP8 DataBound app with an ItemViewModel
bound to a LongListSelector
.
很简单,当用户点击列表中的一个项目时,我需要检索选择以供将来使用的项目的索引号。 (0是列表中的第一,1是第二个等)
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();
我认为 SelectedIndex
属性是我需要,但我无法弄清楚如何检索。
I think the SelectedIndex
property is the thing I need but I can't figure out how retrieve it.
谢谢!
编辑:解决!以下是我正在寻找的东西:
SOLVED! The following gets me exactly what I was looking for:
int selectedIndex = App.ViewModel.Items.IndexOf(MainLongListSelector.SelectedItem as ItemViewModel);
推荐答案
我也有同样的问题。您需要使用ItemSource来检索索引。它应该匹配您的索引的数据模板索引。
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);
选择器引用LongListSelector对象发件人。希望这有帮助!
selector references the LongListSelector object sender. Hope this helps!
这篇关于获取一个LongListSelector Item的SelectedIndex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!