简体   繁体   English

这样的物体有名字吗? 我将如何用它填充树视图?

[英]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:我将如何用它填充 wpf 树视图,使其看起来像这样:

parent0
   child0
   child1
       child0ofchild1
    .
    .
    .

I want this to be able to be infinitly long so a fixed for-loop is not an option.我希望它能够无限长,因此固定的 for 循环不是一种选择。 foreach worked quite well but i run into problems when going down more than 2 levels in this tree. foreach 工作得很好,但是当我在这棵树中下降超过 2 个级别时遇到了问题。 Is there something simpler already in c# or am I on the right track? c# 中是否已经有一些更简单的东西,或者我是否在正确的轨道上?

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.如果绑定到 WPF 树,我将使用 ObservableCollection 而不是 List 作为子项。 I have made a simple example with a recursive function building up a tree, and then displaying it in a WPF TreeView.我做了一个简单的例子,用递归函数构建一棵树,然后在 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):这是填充树的递归函数(它生成 5 代子节点,每个父节点有 6 个子节点):

        /// <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): XAML 标记(有关基本教程,请参阅此链接):

    <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.当然这只是一个简单的例子,我会使用视图模型和数据绑定等,但这超出了这个问题的范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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