本篇要實現的目標,簡單來講就是把一個DevExpress treelist的節點用鼠標拖到另外的節點(自身或其餘的listview)上,以下圖:html
(圖一)node
(圖二)express
(圖三)code
首先,在窗口中拉入兩個listview,第一個建立三列(上),第二個建立兩列(下),以下圖:htm
爲第一個listview建立一些節點:對象
定義一個取得拖拽對象中節點的方法:blog
private TreeListNode GetDragNode(IDataObject data) { return (TreeListNode)data.GetData(typeof(TreeListNode)); }
在兩個treelist中同時定義DragDrop(鼠標鬆開)和DragEnter(開始拖拽)事件:事件
private void treeList1_DragDrop(object sender, DragEventArgs e) { TreeListNode node = GetDragNode(e.Data); if (node == null) return; TreeList list = (TreeList)sender; if (list == node.TreeList) return; TreeListHitInfo info = list.CalcHitInfo(list.PointToClient(new Point(e.X, e.Y))); InsertBrush(list,node,info.Node==null?-1:info.Node.Id); } private void treeList1_DragEnter(object sender, DragEventArgs e) { TreeList list = (TreeList)sender; TreeListNode node = GetDragNode(e.Data); if (node != null && node.TreeList != list) e.Effect = DragDropEffects.Copy; }
最後定義插入節點的方法: get
private void InsertBrush(TreeList list, TreeListNode node, int parent) { ArrayList data = new ArrayList(); foreach (TreeListColumn column in node.TreeList.Columns) { data.Add(node[column]); } parent = list.AppendNode(data.ToArray(), parent).Id; if (node.HasChildren) foreach (TreeListNode n in node.Nodes) InsertBrush(list, n, parent); }
如今就能夠用鼠標實現兩個列表的互拖了:it
By螞蟻拉車
擴展閱讀: