开发者

Is there an easy way to navigate a List<t> object?

I have a WPF form that shows a contact (Name, Address and State).

The GUI is bound to a CurrentContact object and they are stored in a List&开发者_JAVA百科lt;Contact>.

I would like to add buttons to the bottom:

+-----+  +-----+  +-----+  +-----+ 
| <<  |  |  <  |  |  >  |  | >>  |
+-----+  +-----+  +-----+  +-----+  

Meaning first, previous, next and last.

Is there an easy control or convention to iterate through the list? Or do I need to store an currentItemIndex and roll my own?


Lists provide random access, so you don't need to iterate through them to get from one spot to another. In fact, it's probably inefficient to iterate if the list is very long; imagine you wanted to get to the last record from the first one, for example.

In any case, your four buttons would just be:

  • first: list[0]
  • previous: list[currentIndex - 1]
  • next: list[currentIndex + 1]
  • last: list[list.Count - 1]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜