【发布时间】:2010-08-22 22:24:56
【问题描述】:
我正在使用 List 构造来处理在“OnPaint”中绘制图像的顺序。现在,如果我的图像被重新排序(例如“带到前面”或“.. 到后面”),我需要在我的列表中重新定位它们。 我这样做有困难,因为 List 不支持类似于 setIndex() 的方法。
所以我想做的基本上是:
private List<BitmapWithProps> activeImages = new List<BitmapWithProps>();
public void addActiveImage(BitmapWithProps image)
{
activeImages.Add(image);
}
public BitmapWithProps getActiveImage(int index)
{
return activeImages[index];
}
public void removeActiveImage(int index)
{
activeImages.RemoveAt(index);
}
public void removeActiveImage(BitmapWithProps item)
{
activeImages.Remove(item);
}
public void swapActiveImageIndex(int sourceIndex, int destIndex)
{
// what would the code look like in here if I were to swap
// the 2nd item (1) with the 4th one (3) in a 5-item-List (0 - 4)
}
我希望能够交换索引.. 有点。我可以在它应该去的索引处将一个新项目“插入”到列表中并分配值,然后删除另一个“源”。然而,它看起来并不优雅。
我很乐意提供任何提示,如果我忽略了一个线程,请原谅 - 我在询问之前进行了搜索。
dS.
【问题讨论】: