Reputation: 33
I have a TreeView
. Its TreeNodes
have a TreeNode.Tag
property of type InfoForNode
. It contains field public DataGridViewRow[] Properties
where I store all information about node.
Now I want to copy this node (with all its properties from Node.Tag
) and paste it in the same TreeView
. I tried to use TreeNode.Clone()
method:
foreach (var node in TreeQuery.SelectedNodes)
{
_copiedNodes.Add((TreeNode)node.Clone());
}
It works, but copied and original nodes point at the same instance of DataGridViewRow[] Properties
(Because its a Reference type and Clone()
performes a shallow copy of a node).
Please explain, how can I get a proper copy in this case?
Upvotes: 1
Views: 493
Reputation: 1765
The easiest way would be to do this manually.
foreach (var node in TreeQuery.SelectedNodes)
{
TreeNode newNode = (TreeNode)node.Clone();
DataGridView[] oldProperties = (DataGridView[])node.Tag;
DataGridView[] newProperties = new DataGridView[oldProperties.Length];
for(int i = 0; i < oldProperties.Length; i++)
{
newProperties[i] = oldProperties[i].Clone(); //or whatever copy method works for this
}
_copiedNodes.Add(newNode);
}
This gives you complete control over how you copy the tag data.
Upvotes: 1