Reputation: 2783
I have a collection of TreeListNodes. I am doing this to copy all nodes to another tree.
foreach (TreeListNode tn in nodes)
trTree.Nodes.Add(tn);
Above code works, but expand-collapse of nodes in tree won't work after this.
How can I copy one XtraTreelist to another ?
Upvotes: 0
Views: 957
Reputation: 75306
It will not work this way, you are sharing nodes between trees with same memory location. Think about if you change data on Tree 1, it will affect immediately to Tree 2.
It would be suggested to do Deep Copy on TreeListNode
with DeepClone
method:
public static class CloneHelper
{
public static T DeepClone<T>(T obj)
{
using (var ms = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
ms.Position = 0;
return (T) formatter.Deserialize(ms);
}
}
}
Then you can use:
var cloneNodes = nodes.Select(node => CloneHelper.DeepClone<TreeListNode>(node))
.ToArray();
trTree.Nodes.AddRange(cloneNodes);
Edit:
Of course you need to make sure TreeListNode
is marked as [Serializale]
Another approach, you need to re-build the second tree with the data used in the first tree
Upvotes: 1