简体   繁体   中英

Is there a name for an object like this? And how would I go about populating a treeview with it?

I use an object structure like this:

class Node
{
    string name;
    List<Node> children = new List<Node>{};
    Node parent;
}

I can add children or nodes at the same level as the current node.

Is there a term for objects like that?

How would I fill a wpf tree view with this so it would look like this:

parent0
   child0
   child1
       child0ofchild1
    .
    .
    .

I want this to be able to be infinitly long so a fixed for-loop is not an option. foreach worked quite well but i run into problems when going down more than 2 levels in this tree. Is there something simpler already in c# or am I on the right track?

Sorry if this is a repetetive question but I dont know the right term to google...

To traverse through a tree, a recursive function is the universal solution. If binding to a WPF tree, I would use ObservableCollection instead of List for the child items. I have made a simple example with a recursive function building up a tree, and then displaying it in a WPF TreeView. This is the data class:

    public class TreeNode {
        public ObservableCollection<TreeNode> Children {
            get;
        } = new ObservableCollection<TreeNode>();

        public string Name {
            get;set;
        }
    }

This is the recursive function to fill the tree (it generates 5 generations of children, each parent node having 6 children):

        /// <summary>
        /// Fill a recursive tree
        /// </summary>
        /// <param name="node">The treenode to generate children for</param>
        /// <param name="depth">Current depth, always incremented by 1 when going deeper in recursion</param>
        /// <param name="namePostfix">Name postfix, just for generating display name for the nodes</param>
        void fillTree(TreeNode node, int depth, string namePostfix) {
            // exit to prevent endless recursion
            if (depth >= 5) {
                return;
            }
            for (int i = 0; i < 6; ++i) {
                var child = new TreeNode() {
                    Name = $"Node{namePostfix}{i}"
                };
                fillTree(child, depth + 1, namePostfix + i);
                node.Children.Add(child);
            }
        }

If you want to traverse the tree, you should write something similar according to the traverse method you choose (from the previous answer).

XAML markup (see this link for a basic tutorial):

    <TreeView x:Name="tv">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate DataType="{x:Type local:TreeNode}" ItemsSource="{Binding Children}">
                <TextBlock Text="{Binding Name}" />
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

And in the constructor of the Window/Page:

            fillTree(tree, 0, "");
            tv.Items.Add(tree);  // tv is the x:Name of the TreeView

Of course this is just a simple example, I would use view model and data binding etc, but that is out of the scope of this question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM