2011-11-21 127 views
1

由於標題真的 - 我有一些代碼用於重新排列ListView中的一些縮略圖('LargeIcon'視圖格式),並且在拖拽圖像到正確的(即更大的指數)。但是,拖到左邊時根本不起作用。ListView Drag/Drop重新排序 - 在拖拽左邊時不起作用

任何想法?

private void lstImages_ItemDrag(object sender, ItemDragEventArgs e) 
    { 
     lstImages.DoDragDrop(lstImages.SelectedItems, DragDropEffects.Move); 
    } 

    private void lstImages_DragEnter(object sender, DragEventArgs e) 
    { 
     if (e.Data.GetDataPresent(typeof(ListView.SelectedListViewItemCollection))) 
     { 
      e.Effect = DragDropEffects.Move; 
     } 
    } 

    private void lstImages_DragDrop(object sender, DragEventArgs e) 
    { 
     if (lstImages.SelectedItems.Count == 0) return; 

     Point p = lstImages.PointToClient(new Point(e.X, e.Y)); 
     ListViewItem dragToItem = lstImages.GetItemAt(p.X, p.Y); 

     if (dragToItem == null) return; 

     int dragIndex = dragToItem.Index; 

     ListViewItem dataItem = (e.Data.GetData(typeof(ListView.SelectedListViewItemCollection)) as ListView.SelectedListViewItemCollection)[0]; 
     int itemIndex = dragIndex; 
     if (itemIndex == dataItem.Index) return; 

     itemIndex = itemIndex + 1; 

     ListViewItem insertItem = (ListViewItem)dataItem.Clone(); 
     lstImages.Items.Insert(itemIndex, insertItem); 
     lstImages.Items.Remove(dataItem); 
    } 

回答

2

我修改了一下你的代碼,它現在可以工作了。 (只是_DragDrop部分)。似乎有一個錯誤,它可以通過在拖放拖放期間更改對齊來修復。

listView1.Alignment = ListViewAlignment.Default; 
    if (listView1.SelectedItems.Count == 0) 
    return; 
    Point p = listView1.PointToClient(new Point(e.X, e.Y)); 
    ListViewItem MovetoNewPosition = listView1.GetItemAt(p.X, p.Y); 
    if (MovetoNewPosition == null) return; 
    ListViewItem DropToNewPosition = (e.Data.GetData(typeof(ListView.SelectedListViewItemCollection)) as ListView.SelectedListViewItemCollection)[0]; 
    ListViewItem CloneToNew = (ListViewItem)DropToNewPosition.Clone(); 
    int index = MovetoNewPosition.Index; 
    listView1.Items.Remove(DropToNewPosition); 
    listView1.Items.Insert(index, CloneToNew); 
    listView1.Alignment = ListViewAlignment.SnapToGrid; 
+0

這是否解決了您的問題? – Alex

+0

它確實修復它! – hawbsl