3

我有一个像这样的输入流:

John
Peter
Vanesa
Vanesa.New
Josh
Josh.New
Josh.New.Under
...

我需要像这样将节点添加到 TreeView Someting:

+Customers
   +John
   +Peter
   +Vanesa
      +New
   +Josh
      +New
         +Under
 ...

我有一个想法用参数'。'分割每个字符串,但我遇到了动态加载节点的问题。也许我必须使用某种 foreach ......

我有带有记录 id 和 GroupName 的旧数据库表“组”。都充满了这些字符串。我需要创建某种“地址”,例如:John.Element 或 Vanesa.New.Element 或 Josh.New.Under.Element,其中 Element 是从其他数据表中记录的。DB连接不是问题,问题是动态填充树

现在我已经完成了添加不包含“。”的字符串:

    reader = readGroups.ExecuteNonQuery();
    while(reader.Read())
    {
        string[] buff = reader.GetValue(1).ToString().Split('.');
        if (buff.Length == 1)
        {
            treeView1.Nodes[0].Nodes.Add(reader.GetValue(1));
        }
        else
        {
            //group contains '.'
        }
    }

编辑:我还有一个问题。有这样的记录:John, John.New, John.Old, John.Older, John.Oldest ... 所以当AddNodes() 方法运行时,方法末尾的foreach 会清除John.New, John。 Old, John.Older 节点,但他们必须进入树节点 John。如果你有什么想法...

4

4 回答 4

4

对于winforms,这是您需要的。我正在使用递归在每个父节点中添加每个子节点。我已经进行了更改,以便它在开始将任何节点添加到实际树视图之前创建一个唯一节点列表

            internal class TreeNodeHierachy
    {
        public int Level { get; set; }
        public TreeNode Node { get; set; }
        public Guid Id { get; set; }
        public Guid ParentId { get; set; }
        public string RootText { get; set; }
    }

    private List<TreeNodeHierachy> overAllNodeList; 

    private void AddNodes(IEnumerable<string> data)
    {
        overAllNodeList = new List<TreeNodeHierachy>();
        foreach (var item in data)
        {
            var nodeList = new List<TreeNodeHierachy>();
            var split = item.Split('.');
            for (var i = 0; i < split.Count(); i++)
            {
                var guid = Guid.NewGuid();
                var parent = i == 0 ? null : nodeList.First(n => n.Level == i - 1);
                var root = i == 0 ? null : nodeList.First(n => n.Level == 0);
                nodeList.Add(new TreeNodeHierachy
                    {
                        Level = i,
                        Node = new TreeNode(split[i]) { Tag = guid },
                        Id = guid,
                        ParentId = parent != null ? parent.Id : Guid.Empty,
                        RootText = root != null ? root.RootText : split[i]
                    });
            }

            // figure out dups here
            if (!overAllNodeList.Any())
            {
                overAllNodeList.AddRange(nodeList);
            }
            else
            {
                nodeList = nodeList.OrderBy(x => x.Level).ToList();
                for (var i = 0; i < nodeList.Count; i++)
                {

                    var existingNode = overAllNodeList.FirstOrDefault(
                        n => n.Node.Text == nodeList[i].Node.Text && n.Level == nodeList[i].Level && n.RootText == nodeList[i].RootText);
                    if (existingNode != null && (i + 1) < nodeList.Count)
                    {

                        nodeList[i + 1].ParentId = existingNode.Id;
                    }
                    else
                    {
                        overAllNodeList.Add(nodeList[i]);
                    }
                }
            }
        }

        foreach (var treeNodeHierachy in overAllNodeList.Where(x => x.Level == 0))
        {
            treeView1.Nodes.Add(AddChildNodes(treeNodeHierachy));
        }
    }

    private TreeNode AddChildNodes(TreeNodeHierachy node)
    {
        var treeNode = node.Node;
        foreach (var treeNodeHierachy in overAllNodeList.Where(n => n.ParentId == node.Id))
        {
            treeNode.Nodes.Add(AddChildNodes(treeNodeHierachy));
        }
        return treeNode;
    }


    /// <summary>
    /// Handles the Click event of the button1 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    private void button1_Click(object sender, EventArgs e)
    {
        //SearchActiveDirectoryWithCriteria("(mailnickname=TM418)");

        var test = new List<string>
            {
                "John",
                "Peter",
                "Vanesa",
                "Vanesa.New",
                "Josh",
                "Josh.New",
                "Josh.New.Under",
                "Josh.Old"
            };

        AddNodes(test);           
    }
于 2013-04-24T15:50:41.760 回答
1

这可能主要做你想做的事,你还需要一些带有 TreeView 的 xaml,称为treeView

    public TreeViewItem root;

    public MainWindow()
    {
        InitializeComponent();

        root = new TreeViewItem
        {
            Header = "Customers"
        };

        treeView.Items.Add(root);

        addNode("John");
        addNode("Peter");
        addNode("Vanesa.New");
        addNode("Josh");
        addNode("Josh.New");
        addNode("Josh.New.Under");
    }

    private void addNode(string values)
    {
        var n = root;

        foreach (var val in values.Split('.'))
        {
            var isNew = true;

            foreach (var existingNode in n.Items)
            {
                if (((TreeViewItem)existingNode).Header.ToString() == val)
                {
                    n = (TreeViewItem)existingNode;
                    isNew = false;
                }
            }

            if (isNew)
            {
                var newNode = new TreeViewItem
                {
                    Header = val
                };

                n.Items.Add(newNode);

                n = newNode;
            }
        }
    }
于 2013-04-24T13:43:22.710 回答
0

回复@charles380 解决方案:我已更新代码以使用 WPF .Net Core 树视图。

public class TreeNodeHierarchy
    {
        public int Level { get; set; }
        public TreeViewItem Node { get; set; }
        public Guid Id { get; set; }
        public Guid ParentId { get; set; }
        public string RootText { get; set; }
    }
    public class TreeManager{

        public List<TreeNodeHierarchy> overAllNodeList;

        public void AddNodes(IEnumerable<string> data, ref TreeView treeView1)
        {
            overAllNodeList = new List<TreeNodeHierarchy>();
            foreach (var item in data)
            {
                //\test
                var nodeList = new List<TreeNodeHierarchy>();
                var split = item.Split('.', StringSplitOptions.RemoveEmptyEntries);
                for (var i = 0; i < split.Count(); i++)
                {
                    var guid = Guid.NewGuid();
                    var parent = i == 0 ? null : nodeList.First(n => n.Level == i - 1);
                    var root = i == 0 ? null : nodeList.First(n => n.Level == 0);
                    nodeList.Add(new TreeNodeHierarchy
                    {
                        Level = i,
                        Node = new TreeViewItem { Header = split[i],   Tag = guid },
                        Id = guid,
                        ParentId = parent != null ? parent.Id : Guid.Empty,
                        RootText = root != null ? root.RootText : split[i]
                    });
                }

                // figure out dups here
                if (!overAllNodeList.Any())
                {
                    overAllNodeList.AddRange(nodeList);
                }
                else
                {
                    nodeList = nodeList.OrderBy(x => x.Level).ToList();
                    for (var i = 0; i < nodeList.Count; i++)
                    {

                        var existingNode = overAllNodeList.FirstOrDefault(
                            n => n.Node.Header.ToString() == nodeList[i].Node.Header.ToString() && n.Level == nodeList[i].Level && n.RootText == nodeList[i].RootText);


                        if (existingNode != null && (i + 1) < nodeList.Count)
                        {

                            nodeList[i + 1].ParentId = existingNode.Id;
                        }
                        else
                        {
                            overAllNodeList.Add(nodeList[i]);
                        }
                    }
                }
            }

            foreach (var treeNodeHierachy in overAllNodeList.Where(x => x.Level == 0))
            {
                treeView1.Items.Add(AddChildNodes(treeNodeHierachy));
            }
        }

        private TreeViewItem AddChildNodes(TreeNodeHierarchy node)
        {
            var treeNode = node.Node;
            foreach (var treeNodeHierachy in overAllNodeList.Where(n => n.ParentId == node.Id))
            {
                treeNode.Items.Add(AddChildNodes(treeNodeHierachy));
            }
            return treeNode;
        }
    }
}

示例调用代码:

    var lstFolders= new List<string>
            {
                "John",
                "Peter",
                "Vanesa",
                "Vanesa.New",
                "Josh",
                "Josh.New",
                "Josh.New.Under",
                "Josh.Old"
            };
TreeManager treeManager = new TreeManager();
treeManager.AddNodes(lstFolders, ref tr);
于 2020-03-31T16:51:35.323 回答
0

我有同样的问题。我以这种方式解决了这个问题:

定义一个实现树的类:

class TreeBuilder
{
    public int index,depth;
    public string text;
    public Dictionary<string,TreeBuilder> childs;
    public void addToTreeVeiw(System.Windows.Forms.TreeNode root, TreeBuilder tb) {
        foreach (string key in tb.childs.Keys) {
            System.Windows.Forms.TreeNode t = root.Nodes.Add(tb.childs[key].text);
            addToTreeVeiw(t, tb.childs[key]);

        }
    }
}

主要部分:

string[] lis = {"a","b","a.a","a.ab","c","cc.a","a.b.dd","samad.hah.hoh"};
            treeView1.Nodes.Clear();
            TreeBuilder Troot = new TreeBuilder();
            TreeBuilder son;
            Troot.depth = 0;
            Troot.index = 0;
            Troot.text = "root";
            Troot.childs = new Dictionary<string, TreeBuilder>();

            foreach (string str in lis)
            {
                string[] seperated = str.Split('.');
                son = Troot;
                int index= 0;
                for (int depth = 0; depth < seperated.Length; depth++)
                {
                    if (son.childs.ContainsKey(seperated[depth]))
                    {
                        son = son.childs[seperated[depth]];
                    }
                    else {
                        son.childs.Add(seperated[depth],new TreeBuilder());
                        son = son.childs[seperated[depth]];
                        son.index= ++index;
                        son.depth = depth+1;
                        son.text = seperated[depth];
                        son.childs = new Dictionary<string, TreeBuilder>();
                    }
                }
            }
            treeView1.Nodes.Add("root");
            Troot.addToTreeVeiw(treeView1.Nodes[0], Troot);

我想结果就是你想要的:

在此处输入图像描述

于 2015-12-07T18:55:39.080 回答