【技术分享】【C#】树列表的节点拖拽
琛
帖子
14
14
精华
1
1
积分
154
154
二次开发
技术分享
树列表的拖拽涉及三个NX的回调,需要在三个回调设置好规则之后才能进行拖拽操作。
tree_control0.SetOnDropHandler(new Tree.OnDropCallback(OnDropCallback));
tree_control0.SetIsDragAllowedHandler(new Tree.IsDragAllowedCallback(IsDragAllowedCallback));
tree_control0.SetIsDropAllowedHandler(new Tree.IsDropAllowedCallback(IsDropAllowedCallback));
1.设置节点是否允许拖拽,下方设置的为父节点禁止拖拽。
internal Node.DragType IsDragAllowedCallback(Tree tree, Node node, int columnId)
{
//是否允许拖动回调
if (node.ParentNode != null)
{
return Node.DragType.All;
}
return Node.DragType.None;
}
2.设置是否执行拖拽回调,执行的方式,下方为目标节点为父节点时,作为子节点放置到其中,如果有父节点,则放到该节点后面。
/// <summary>
/// 是否执行拖拽回调
/// </summary>
/// <param name="tree"></param>
/// <param name="node">拖拽的节点</param>
/// <param name="columnId"></param>
/// <param name="targetNode">或拽到的目标节点</param>
/// <param name="targetColumnId"></param>
/// <returns></returns>
public Node.DropType IsDropAllowedCallback(Tree tree, Node node, int columnId, Node targetNode, int targetColumnId)
{
//是否执行拖拽回调
//SessionEx.WriteInformationWindow($"{targetNode.GetColumnDisplayText(0)}__{targetColumnId}");
var dropType = targetNode.ParentNode == null ? Node.DropType.On : Node.DropType.After;
return dropType;
}
3.拖拽回调,拖拽发生后执行此处的内容,需要手工新建与移除拖拽后目标节点。
internal bool OnDropCallback(Tree tree, Node[] nodes, int columnId, Node targetNode, int targetColumnId, Node.DropType dropType, int dropMenuItemId)
{
//拖动回调
foreach (var node in nodes)
{
var nodeData = NodeDataManage.First(x => x.Node == node);
Node newNode = tree.CreateNode(node.GetColumnDisplayText(0));
if (dropType == Node.DropType.On)
{
tree.InsertNode(newNode, targetNode, null, Tree.NodeInsertOption.Last);
}
else if (dropType == Node.DropType.After)
{
var parent = targetNode.ParentNode;
tree.InsertNode(newNode, parent, targetNode, Tree.NodeInsertOption.First);
}
newNode.SetColumnDisplayText(1, node.GetColumnDisplayText(1));
newNode.SetColumnDisplayText(2, node.GetColumnDisplayText(2));
newNode.DisplayIcon = node.DisplayIcon;
newNode.SelectedIcon = node.SelectedIcon;
tree.DeleteNode(node);
}
return true;
}
登录论坛用户后可查看全部内容
477
0
2024-04-19 10:43:10
by f3dexpress Nico
请选择移动至版块:
确认移动
回复加入讨论