gpt4 book ai didi

c# - 从 ListCollectionView 获取内部项目编号

转载 作者:太空宇宙 更新时间:2023-11-03 16:49:45 25 4
gpt4 key购买 nike

我像这样扩展了 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中的index内部的collections index呢?某处不应该有一个“int ConvertToInternalIndex(int index)”吗?

最佳答案

我猜这是因为 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;
}

关于c# - 从 ListCollectionView 获取内部项目编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4399751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com