【问题标题】:Getting the SelectedIndex of a LongListSelector Item获取 LongListSelector 项的 SelectedIndex
【发布时间】:2013-05-13 02:27:11
【问题描述】:

我有一个 WP8 DataBound 应用程序,其 ItemViewModel 绑定到 LongListSelector

很简单,当用户点击列表中的某个项目时,我需要检索所选项目的索引号以供将来使用。 (0 排在第一位,1 排在第二位,以此类推)

所以,正如这可能会检索所选项目的某个属性:

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

我需要这样的东西(显然是编造的代码):

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

我认为SelectedIndex 属性是我需要的,但我不知道如何检索它。

谢谢!

编辑:已解决!以下内容正是我想要的:

 int selectedIndex = App.ViewModel.Items.IndexOf(MainLongListSelector.SelectedItem as ItemViewModel);

【问题讨论】:

  • 这总是为我返回-1。知道可能是什么问题吗?
  • 这里相同..总是返回-1

标签: c# silverlight windows-phone-7 data-binding


【解决方案1】:

我遇到了同样的问题。您需要使用 ItemSource 来检索索引。它应该与您的数据模板索引相匹配。

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

selector 引用 LongListSelector 对象发送者。希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    • 1970-01-01
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多