views:

535

answers:

4

Hi,

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);
+1  A: 

He wants to know how to get a copy of an object, rather than a copy of a reference to an object.

Robert
+1  A: 

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.

Greg B
+1  A: 

There are some Reflection based methods you can use to deep-copy a custom object, google for "Reflection deep copy". This article, for example: http://alpatrick.blogspot.com/2005/01/prototype-pattern-using-reflection.html, clones all properties marked with a custom attribute [DeepCopy], but you can easily change that behavior yourself.

Also, there is a similar thread on SO: http://stackoverflow.com/questions/569154/how-to-deep-copy-between-objects-of-different-types-in-c-net

[Edit] Note than in your example it may not be wise to simply create a complete deep copy of your tree node. A node may contain references to the tree, and other internal stuff which could possibly be unique and not copied to a different node.

In that case, you should simply create a new node (using a constructor), assign the data from the source node, and then add the new node to the tree.

The best way overall would be to bind your tree to you data, so that you never manually change the tree, but make it update automatically whenever you change your data. Check your Infragistics documentation on "data binding", that should be the best approach.

Groo
A: 

You can use the SaveToBinary() method of UltraTree to clone.

the code should look like this:

void CloneTree() 
{
            //save the current tree into stream
            var savedTree = new MemoryStream();
            ultraTree1.SaveAsBinary(savedTree);
            byte[] buffer = savedTree.ToArray();
            savedTree.Close()

            //create a clone from the stream
            UltraTree newTree = new UltraTree();
            newTree.LoadFromBinary(new MemoryStream(buffer));
}

I hope this helps :-)

Shai Rubinshtein