泛型列表 - 在列表中移动项目 [英] Generic List - moving an item within the list

查看:166
本文介绍了泛型列表 - 在列表中移动项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个通用的列表,以及 oldIndex进行 newIndex 值。

So I have a generic list, and an oldIndex and a newIndex value.

我想移动的项目在 oldIndex进行,到 newIndex ...尽可能简单。

I want to move the item at oldIndex, to newIndex...as simply as possible.

有什么建议?

该项目应该是最终的项目之间的(newIndex - 1) newIndex 之前, 的它已被删除。

The item should be end up between the items at (newIndex - 1) and newIndex before it was removed.

推荐答案

我知道你说的泛型列表但你没有指定你需要使用列表(T)的类,这里是一杆优势在不同的东西。

I know you said "generic list" but you didn't specify that you needed to use the List(T) class so here is a shot at something different.

的ObservableCollection(T)的类有一个移动方法这不正是你想要的。

The ObservableCollection(T) class has a Move method that does exactly what you want.

public void Move(int oldIndex, int newIndex)

它的下面,基本上实现这样的。

Underneath it is basically implemented like this.

T item = base[oldIndex];
base.RemoveItem(oldIndex);
base.InsertItem(newIndex, item);

因此​​,大家可以看到交换方法,其他人都建议基本上是什么的的ObservableCollection 的做在它自己的移动方式。

So as you can see the swap method that others have suggested is essentially what the ObservableCollection does in it's own Move method.

这篇关于泛型列表 - 在列表中移动项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆