Get internal item no from ListCollectionView
I've extended a ListCollectionView and overridded GetItemAt like this:
public class LazyLoadListCollectionView : ListCollectionView
{
public override object GetItemAt(int index)
{
object rc = base.GetItemAt(index);
// do something
return rc;
}
}
Now for my "do something" I need the position of the item in the internal list. As long as the ListCollectionView is not sorted "index" of the ListCollectionView will be th开发者_运维问答e same for the internal collection, but as soon as the ListCollectionView is resorted index will match the index in the internal collection (the internal collection being an ObservableCollection).
So where does ListCollectionView get the internal collections index from the index in the ListCollectionView? Shouldn't there be a "int ConvertToInternalIndex(int index)" somewhere?
I guess it's because SourceCollection for ListCollectionView is of type IEnumerable. To get the index in the SourceCollection you could try to cast it to an IList and use IndexOf. To get index from IEnumerable see this question
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;
}
精彩评论