Search code examples
c#infragisticsultratree

Need UltraTree Clone Method - Problem with reference


I have ultraTree (infragistics tree) which is created at design time and it has custom class as "Tag". I have List as member variable. I have two combo box. Based on the combo selection I'll check the List's each items "Tag". If list contains, just i pick that, otherwise i create a new UltraTree and add it to List and i assign to the Tree that is created at design time.

My problem is, what i adding to the collection gets reference and all the item in the collection are overwritten by the last item. UltraTree don't have any clone method.

I don't found any way to clone using this control.

What can be my solution :(

My sample code is

// My custom class
SelectedDeviceState treeState = new SelectedDeviceState(
    firstDevice, secondDevice);

UltraTree tree = new UltraTree();

// This will clone the root node
// (it will be executed only once -> Root)
foreach (UltraTreeNode nod in tvwNavigation.Nodes)
{
    UltraTreeNode tnode = nod.Clone() as UltraTreeNode;
    tree.Nodes.Add(tnode);
}

//Adding the custom class as TAG
tree.Tag = treeState;

// Assigned and added
tvwNavigation = tree;
_treeCollection.Add(tree);

Solution

  • You could perform a manual clone by writing a method that creates a new isntance of your type then assigns values to it from the original object.

    In terms of a tree structure you would need to write a recursive clone, something like.

    private ItemType CloneDeep(ItemType node)
    {
        ItemType clone = new ItemType();
        clone.Property1 = node.Property1;
        clone.Property2 = node.Property2;
    
        foreach ( ItemType child in node.Nodes)
        {
            clone.Nodes.add(CloneDeep(child));
        }
        return clone;
    }
    

    Maybe look at extension methods.