-2

I can't find an answer to this question no matter how I ask it. There appears to be solutions if the treeview is in WinForms but not if it's in WPF.

I'm loading an XML file at runtime that is bound to a treeview called OOB. The user can perform all kinds of edits to the treeview (adding and deleting nodes, changing the node's contents, etc.). All of this works perfectly. Now, I have to save it back out as an XML file. Apparently, this is near impossible in WPF.

At this point all I can think of doing is traversing the entire tree (from what I've read it may be necessary to first expand all nodes which is easily enough done) and then I can, one by one, read the nodes and write them to an XML file using xmlWriter.

So, unless somebody has a better idea, how can I in C# and WPF traverse an entire tree, getting the contents of each Node.name? Once I have the contents of each node I should be able to reconstitute the tree structure and write them out as an XML file.

2
  • Normally one would bind the treeview to some underlying data model and save that data model. See e.g. wpf treeview binding or how to bind dynamic json into treeview wpf or codeproject.com/Articles/26288/…. But I think you should be able to use FindVisualChildren<TreeViewItem>(treeView) from this question to get all TreeViewItem children of a TreeView.
    – dbc
    Apr 30, 2017 at 21:54
  • 1
    Typically, you will find that people comment on down-votes only if it seems necessary. Often, a close-vote suffices to indicate what is missing in a post (and you have two down-votes and two close-votes, so there seems to be parity there). Note that SO questions should be free of anything not directly related to the question at hand. I've edited your question on your behalf to improve it so that it meets the community standards. Based on the evidence at hand, I would say your question earned down-votes by being too broad and not including a good minimal reproducible example. See also How to Ask, including links at end May 1, 2017 at 0:55

1 Answer 1

-3

Try one of these solutions

IEnumerable<TreeNode> Collect(TreeNodeCollection nodes)
{
    foreach(TreeNode node in nodes)
    {
        yield return node;

        foreach (var child in Collect(node.Nodes))
            yield return child;
    }
}

Another solution

protected void TraverseNodes(TreeNodeCollection nodes, string action, int maxDepth = 2) 
    {
        foreach (TreeNode node in nodes)
        {
            if (node.ChildNodes.Count > 0 && node.Depth < maxDepth)
                TraverseNodes(node.ChildNodes, action, maxDepth);

            //do something!!!
            var x = node.Text;
            node.Checked = !node.Checked;
        }
    }
1
  • TreeNodeCollection only exists in WinForms. This is for WPF.
    – zetar
    Apr 30, 2017 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.