码迷,mamicode.com
首页 > 其他好文 > 详细

浅谈DevExpress<四>:TreeList中的拖拽功能

时间:2014-06-06 18:13:47      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   class   blog   code   

  本篇要实现的目标,简单来说就是把一个treelist的节点用鼠标拖到另外的节点(自身或其他的listview)上,如下图:

bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣

  首先,在窗口中拉入两个listview,第一个创建三列(上),第二个创建两列(下),如下图:

bubuko.com,布布扣

  为第一个listview创建一些节点:

bubuko.com,布布扣

  定义一个取得拖拽对象中节点的方法:

 private TreeListNode GetDragNode(IDataObject data)
        {
            return (TreeListNode)data.GetData(typeof(TreeListNode));
        }

  在两个treelist中同时定义DragDrop(鼠标松开)和DragEnter(开始拖拽)事件:

bubuko.com,布布扣
        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;
        }
bubuko.com,布布扣

  最后定义插入节点的方法: 

bubuko.com,布布扣
     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);
        }
bubuko.com,布布扣

  现在就可以用鼠标实现两个列表的互拖了:

bubuko.com,布布扣

 

浅谈DevExpress<四>:TreeList中的拖拽功能,布布扣,bubuko.com

浅谈DevExpress<四>:TreeList中的拖拽功能

标签:des   c   style   class   blog   code   

原文地址:http://www.cnblogs.com/xuekai-to-sharp/p/3766342.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!