如何将TreeListNodes添加到TreeListNode?

时间:2017-05-18 05:28:52

标签: c# devexpress treelist

我想删除节点并重新插入。此代码删除节点,但不添加。是否可以将TreeListNodes (newNodes)添加到TreeListNode (myNode)? 我做错了什么?

TreeListNode myNode = treeList1.FindNode((node) => {
    return node["ColumnName"].ToString() == "test";
});

var child = myNode.Nodes;

TreeListNodes newNodes = new TreeListNodes(treeList1);

for (int i = child.Count - 1; i >= 0; i--) {
    newNodes.Add(child[i]);
    treeList1.DeleteNode((DevExpress.XtraTreeList.Nodes.TreeListNode) child[i]);
}

myNode.Nodes.Add(newNodes);

1 个答案:

答案 0 :(得分:0)

据我所知,您正在尝试以相反的顺序重新排序特定节点的子项。 如果是这样,则不需要添加/删除节点,我建议您使用以下方法(MoveNode):

TreeListNode testNode = treeList1.FindNode(
    node => object.Equals(node["Name"], "test"));
TreeListNodes children = testNode.Nodes;
for(int i = children.Count - 1; i >= 0; i--)
    treeList1.MoveNode(children[i], testNode, false, position: 0);

替代方式(SetNodeIndex):

TreeListNodes children = testNode.Nodes;
for(int i = children.Count - 1; i >= 0; i--)
    treeList1.SetNodeIndex(children[i], 0);

P.S。我已经使用latest(v17.1) version测试了上述方法,它宣布了TreeList API的多项改进。