Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从ListCollectionView获取内部项目编号_C#_Wpf - Fatal编程技术网

C# 从ListCollectionView获取内部项目编号

C# 从ListCollectionView获取内部项目编号,c#,wpf,C#,Wpf,我扩展了ListCollectionView并覆盖了GetItemAt,如下所示: public class LazyLoadListCollectionView : ListCollectionView { public override object GetItemAt(int index) { object rc = base.GetItemAt(index); // do something return rc; } } 现在,对于我的“做点什么”

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

public class LazyLoadListCollectionView : ListCollectionView
{

  public override object GetItemAt(int index)
  {
    object rc = base.GetItemAt(index);
    // do something
    return rc;
  }
}
现在,对于我的“做点什么”,我需要项目在内部列表中的位置。只要ListCollectionView未排序,ListCollectionView的“索引”将与内部集合的“索引”相同,但一旦重新调用ListCollectionView,索引将与内部集合中的索引匹配(内部集合是一个可观察集合)


那么ListCollectionView从ListCollectionView中的索引中从何处获取内部集合索引呢?是否应该在某个地方有一个“int-ConvertToInternalIndex(int-index)”呢?

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


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


. 你想干什么?我认为,您需要首先进行探索,因为它能够适合程序员通常希望在其应用程序中实现的大多数场景。。你想干什么?我认为,您需要首先进行探索,因为它能够适应程序员通常希望在其应用程序中实现的大多数场景。正如我在问题中所写的,源集合是一个可观察集合,实现IList也是如此。@Sam:是的,我看到了:)如果您想要一个更通用的解决方案,我只是想。正如我在问题中所写的,源集合是一个ObservableCollection,实现IList也是如此。@Sam:是的,我看到了:)如果您想要一个更通用的解决方案,我想。
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;
}