开发者

How to swap items in treeview control?

I have a treeview control:

  • Parent Item 1
    • Child Item 1
    • Child Item 2
    • Child Item 3
  • Parent Item 2
    • Child Item 1
    • Child Item 2
    • Child Item 3
  • Parent Item 3
    • Child Item 1
    • Child Item 2
    • Child Item 3

I want to move, for example Parent Item 2, up or down with its child items as well as i want to move child items up/down for its parent level.

p.s. I've done this with database, but it's performance issue to rebind treeview every 开发者_Python百科move operation.


I have used a modified version of the code found here here in the past to drag/drop UI items in a TreeView

Basically it finds the parent control that holds the collection, uses the ItemContainerGenerator to find the container that holds the dragged item, then moves the container to the new location within the parent control


Perhaps a two way binding with your custom object ? You can add a custom sorter for the treeview View model by order property for example...


Couldn't that work?

TreeNode node1 = treeView.Nodes[1];
TreeNode node2 = treeView.Nodes[2];
treeView.Nodes[1] = node2;
treeView.Nodes[2] = node1;


The following example is workable for me. Select a child node and swap with its parent node.

TreeNode currentNode, targetNode;
currentNode = TreeView1.SelectedNode;
targetNode = currentNode.Parent;
if (currentNode.Parent != null)
{
    CopyedTreeNode = (TreeNode)targetNode.Clone();
    CopyedTreeNode02 = (TreeNode)currentNode.Clone();

    targetNode.Text = CopyedTreeNode02.Text;
    targetNode.Tag = CopyedTreeNode02.Tag;
    targetNode.ImageIndex = CopyedTreeNode02.ImageIndex;
    targetNode.SelectedImageIndex = CopyedTreeNode02.SelectedImageIndex;

    currentNode.Text = CopyedTreeNode.Text;
    currentNode.Tag = CopyedTreeNode.Tag;
    currentNode.ImageIndex = CopyedTreeNode.ImageIndex;
    currentNode.SelectedImageIndex = CopyedTreeNode.SelectedImageIndex;

    CopyedTreeNode.Remove();
    CopyedTreeNode02.Remove();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜