我扩展了一个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)。
那么,ListCollectionView从ListCollectionView中的索引中获取内部集合索引的位置呢?不是应该有一个"int index(Int ConvertToInternalIndex)“吗?
发布于 2010-12-10 01:49:10
我猜这是因为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;
}https://stackoverflow.com/questions/4399751
复制相似问题