I want to dynamically construct a TreeView using Node which represents a typical tree node. The Node looks like
class Node
{
public Node(string cont) {
Content = cont;
Children = new List<Node>();
}
public string Content { get; set; }
public List<Node> Children { get; set; }
public bool IsLeaf {
get { return Children.Count == 0; }
}
public bool IsVisible {
get { return true; }
}
}
In order to that that I wrote the simple tree traversal which adds TreeViewItems
void XmlTreeTraversal(DataPoolNode curNode, TreeViewItem curViewNode) {
if (curNode.IsLeaf)
return;
var contentNode = (DataPoolNode)curNode;
foreach (var node in contentNode.Children) {
TreeViewItem childViewNode = AddNewNodeToTreeView(node.Content, curViewNode);
XmlTreeTraversal(node, childViewNode);
}
}
TreeViewItem AddNewNodeToTreeView(string description, TreeViewItem curViewNode) {
TreeViewItem newTVI = new TreeViewItem();
newTVI.Header = description;
curViewNode.Items.Add(newTVI);
return newTVI;
}
The problem with the approach is that data and view are intertwined. So It doesn't meet MVVC. Perhaps, you know another solution for this issue?