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