ListView中移动项目 [英] ListView moving items
问题描述
我还有一个问题,ListView控件:(现在我需要在组移动项目(上,下,到开始,到结束),但ListView控件是在最后总是显示移动的项目。
I have another problem with ListView :( Now I need to move items in group (up, down, to the beginning, to the end), but ListView is displaying moved items always at the end.
下面是示例code移动项目的开始:
Here is sample code for moving item to the beginning:
if (1 == listView1.SelectedItems.Count)
{
ListViewItem item = listView1.SelectedItems[0];
ListViewGroup gp = item.Group;
int index;
index = item.Index;
if (index < listView1.Items.Count)
{
index = 0;
listView1.Items.Remove(item);
item.Group = gp;
listView1.Items.Insert(index, item);
}
}
我试图谷歌找到了一些解决方案,我发现别人(<一个href=\"http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/838f90cd-33d8-4c81-9ed9-85220b511afe\" rel=\"nofollow\">http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/838f90cd-33d8-4c81-9ed9-85220b511afe)谁曾同样的问题和我一样,但他的解决方案是行不通的:(
I tried google to find some solution, and I found someone else (http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/838f90cd-33d8-4c81-9ed9-85220b511afe) who had same problem like me, but his solution is not working :(
我考虑过使用ObjectListView,但我已经修改的ListView魔女现在支持拖放放大器;与Winamp的效果,onScroll事件,滚动同步下降等..和我不想失去这个东西:(
I considered using ObjectListView but I have modified ListView witch now supports drag & drop with WinAmp effect, onScroll events, scrolling synchronization etc.. and I don't want to lose this stuff :(
推荐答案
试试这个:
/// <summary>
/// Move the given item to the given index in the given group
/// </summary>
/// <remarks>The item and group must belong to the same ListView</remarks>
public void MoveToGroup(ListViewItem lvi, ListViewGroup group, int indexInGroup) {
group.ListView.BeginUpdate();
ListViewItem[] items = new ListViewItem[group.Items.Count + 1];
group.Items.CopyTo(items, 0);
Array.Copy(items, indexInGroup, items, indexInGroup + 1, group.Items.Count - indexInGroup);
items[indexInGroup] = lvi;
for (int i = 0; i < items.Length; i++)
items[i].Group = null;
for (int i = 0; i < items.Length; i++)
group.Items.Add(items[i]);
group.ListView.EndUpdate();
}
这篇关于ListView中移动项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!