1
votes

I currently have a data structure that displays file system tree's its basically an object with an array list of like typed objects. I want to display this in a treeview, and need to run some code when the tree view nodes are expanded, collapsed, selected, etc.

My question- is there a design pattern, or method of coupling between the data structure and the treeview that wont require me to search the whole DS tree for the selected node each time the user selects something?

Currently I am searching the data structure for a node with the same Text and Tag property as the selected node each time a relevant tree view event fires. I run into scenarios where if the node is not a leaf I have to go and re-search for its parent node, and I'm concerned about performance with large tree

Keep in mind, the data structure already inherits an object so I cant simply extend the treenode class.

Any help is greatly appreciated

1
I'd like to add, I thought of loading the corresponding data node into the treenode tag property, but since the datanode carries the rest of the children, I felt it might be unnecessarily replicating data. However, thinking through it again, it should only be pointing to the structure, so no data replication. I think this might be a valid solution. If someone could validate that, I'll close this question up.NadTheGuy
Please include all relevant information in the question, not comments section.Neolisk

1 Answers

0
votes

You can still extend TreeNode but using composition to add an extra property that maps to the object that node represents.

You can also move the logic for the child nodes to this tree node rather than have it outside

public class DomainClass { /*...*/ }

public class DomainTreeNode: TreeNode
{
    public DomainClass Element { get; private set; }

    public DomainTreeNode(DomainClass element): base(element.Name)
    {
        Element = element;

        /* iterate on element's children and add them to the node's 
           Childs collection ...*/ 
    }

}