1、设置属性OptionsBehavior
Editable = False,
DragNodes = True
设置完之后,就可以进行基础的节点拖拽了
2、增加拖拽逻辑
主要用到三个函数:
BeforeDragNode()(拖拽前判断选中的节点是否可被拖拽);
DragDrop()(拖拽时判断是否可以拖拽到目标节点的子节点或兄弟节点);
AfterFocusNode()(拖拽成功后的事件)
#region treelist拖拽操作
private void treeList1_BeforeDragNode(object sender, BeforeDragNodeEventArgs e)
{
TreeListNode moveNode = treeList1.FocusedNode;
string test = moveNode.GetValue(0).ToString();
//节点"测试1"不能被拖拽
if (moveNode.GetValue(0).ToString() == "测试1")
{
e.CanDrag = false;
}
}
private void treeList1_DragDrop(object sender, DragEventArgs e)
{
System.Drawing.Point p = new System.Drawing.Point(MousePosition.X, MousePosition.Y);
System.Drawing.Point npt = this.treeList1.PointToClient(p);
TreeListNode selectNode = this.treeList1.CalcHitInfo(npt).Node;
//不能拖拽成为节点“测试2”的子节点
if (selectNode.GetValue(0).ToString() == "测试2")
{
Rectangle focuseNodeBounds = treeList1.ViewInfo.RowsInfo[treeList1.FocusedNode].Bounds;//选中节点
Rectangle targetNodeBounds = treeList1.ViewInfo.RowsInfo[selectNode].Bounds;
int offset = targetNodeBounds.Height / 3;
if (!(npt.Y >= targetNodeBounds.Top && npt.Y <= targetNodeBounds.Top + offset))
{
e.Effect = DragDropEffects.None;
}
//如果选中节点在目标节点下方
if (focuseNodeBounds.Top > targetNodeBounds.Top)
{
if (!(pt.Y >= targetNodeBounds.Top && pt.Y <= targetNodeBounds.Top + offset))
{
//不能移动到图层节点的子节点
e.Effect = DragDropEffects.None;
}
}
else
{
if (!(pt.Y <= targetNodeBounds.Bottom && pt.Y >= targetNodeBounds.Bottom - offset))
{
//不能移动到图层节点的子节点
e.Effect = DragDropEffects.None;
}
}
}
}
private void treeList1_AfterDragNode(object sender, NodeEventArgs e)
{
TreeListNode moveNode = treeList1.FocusedNode.ParentNode;//拖拽后的父节点
//拖拽后事件
}
#endregion