Well, besides the tree, you will have some basic data. For example, a directory tree. Directory attributes are its name and a list of child directories. Let's start by defining a common TreeItem .
public class TreeItem<T> { public TreeItem() { Children = new List<TreeItem<T>>(); } public void AddChild(T data) { Children.Add(new TreeItem<T>{Data = data, Parent = this}); } public List<TreeItem<T>> Children{get;set;} public TreeItem<T> Parent {get;set;} public T Data {get;set;} }
So, a simple directory tree is just a TreeItem<string> :
var directories = new TreeItem<string> { Data="root" }; directories.AddChild("child1"); directories.AddChild("child2"); directories.AddChild("child3");
This will create a tree like this:
root |- child1 |- child2 |- child3
The only way to create a fully general tree view is to have the same types for the current node, node above and all child nodes, otherwise you must fix the structure at compile time and only support a set of hierarchies.
Igor Zevaka
source share