2

これは非常に単純かもしれませんが、解決策を思いつくことができませんでした。

私は持っています:

ObservableCollection<ProcessModel> _collection = new ObservableCollection<ProcessModel>();

このコレクションには、多くの ProcessModel が含まれています。

私の質問は、_collection で見つけたい ProcessModel があるということです。

これを行いたいので、ProcessModel が _collection 内にあった場所のインデックスを見つけることができます。これを行う方法が本当にわかりません。

ObservableCollection(_collection)でProcessModel N + 1を先に取得したいので、これを行いたいです。

4

3 に答える 3

8
var x = _collection[(_collection.IndexOf(ProcessItem) + 1)];
于 2012-05-16T12:26:03.390 に答える
5

http://msdn.microsoft.com/en-us/library/ms132410.aspx

使用する:

_collection.IndexOf(_item)

次のアイテムを取得するコードは次のとおりです。

int nextIndex = _collection.IndexOf(_item) + 1;
if (nextIndex == 0)
{
    // not found, you may want to handle this as a special case.
}
else if (nextIndex < _collection.Count)
{
    _next = _collection[nextIndex];
}
else
{
    // that was the last one
}
于 2012-05-16T12:20:25.767 に答える
3

はシーケンスなのでObservableCollection、使用できますLINQ

int index = 
_collection.Select((x,i) => object.Equals(x, mydesiredProcessModel)? i + 1 : -1)
           .Where(x => x != -1).FirstOrDefault();

ProcessModel pm =  _collection.ElementAt(index);

要件に一致するインデックスを 1 にインクリメントしました。

また

ProcessModel pm = _collection[_collection.IndexOf(mydesiredProcessModel) + 1];

また

ProcessModel pm = _collection.ElementAt(_collection.IndexOf(mydesiredProcessModel) + 1);

Not Nullの編集

int i = _collection.IndexOf(ProcessItem) + 1;

var x;
if (i <= _collection.Count - 1) // Index start from 0 to LengthofCollection - 1
    x = _collection[i];
else
    MessageBox.Show("Item does not exist");
于 2012-05-16T12:21:35.170 に答える