从ListCollectionView获取内部项目no

时间:2010-12-09 15:16:31

标签: c# wpf

我扩展了一个ListCollectionView并覆盖了GetItemAt,如下所示:

public class LazyLoadListCollectionView : ListCollectionView
{

  public override object GetItemAt(int index)
  {
    object rc = base.GetItemAt(index);
    // do something
    return rc;
  }
}

现在我的“做某事”我需要内部列表中项目的位置。只要ListCollectionView没有排序,ListCollectionView的“索引”对于内部集合将是相同的,但只要使用ListCollectionView,索引就会匹配内部集合中的索引(内部集合是ObservableCollection)。 / p>

那么ListCollectionView从ListCollectionView中的索引获取内部集合索引呢?不应该在某处有“int ConvertToInternalIndex(int index)”吗?

1 个答案:

答案 0 :(得分:0)

我想这是因为ListCollectionView的SourceCollection属于IEnumerable类型。要获取SourceCollection中的索引,您可以尝试将其强制转换为IList并使用IndexOf。要从IEnumerable获取索引,请参阅this问题

public override object GetItemAt(int index)
{
    object rc = base.GetItemAt(index);
    // do something

    int internalIndex = -1;
    IList sourceCollection = SourceCollection as IList;
    if (sourceCollection != null)
    {
        internalIndex = sourceCollection.IndexOf(rc);
    }
    else
    {
        // See
        // https://stackoverflow.com/questions/2718139
    }
    return rc;
}