【问题标题】:C# WPF: Create TreeView from text fileC# WPF:从文本文件创建 TreeView
【发布时间】:2017-03-06 05:30:38
【问题描述】:

我使用 Node 类创建我的 TreeViewItems。在示例中的节点是在源代码中指定的。但是,如果要从具有如下内容的文本文件中导入节点,我该怎么做:

text file content

有什么想法吗?

我已经尝试了以下方法。

    public MainWindowVM()
    {
        private ObservableCollection<Node> mRootNodes;
        public IEnumerable<Node> RootNodes { get { return mRootNodes; } }
        List<string[]> TreeNodes = new List<string[]>();

        string[] lines = null;
        try
        {
            lines = System.IO.File.ReadAllLines(MainWindow.TextFilePath , System.Text.Encoding.Default);
        }
        catch (IOException ex)
        {
            MessageBox.Show(ex.Message);
            Environment.Exit(0);
        }
        if (lines == null || lines.Length == 0)
        {
            MessageBox.Show("Text file has no content!");
            Environment.Exit(0);
        }

        foreach (var line in lines)
        {
            TreeNodes.Add(line.Split('|'));
        }

        Node newNode = null;
        Node childNode = null;
        Node root = new Node() { Name = TreeNodes[0][0] };
        if (TreeNodes[0].Length > 1)
        {
            newNode = new Node() { Name = TreeNodes[0][1] };
            root.Children.Add(newNode);
        }
        for (int s = 2; s < TreeNodes[0].Length; s++)
        {
            childNode = new Node() { Name = TreeNodes[0][s] };
            newNode.Children.Add(childNode);
            newNode = childNode;
        }
    }

但我只得到前两个节点。我不知道如何用循环构建整个 TreeView。

TreeView

【问题讨论】:

  • 我特别喜欢将文本文件内容发布为屏幕截图的想法。您能否将其发布为 text 并在问题中包含Node 类的代码(链接不正确)
  • 请点击顶部的“节点”。我已经更正了链接。

标签: wpf file text treeview treeviewitem


【解决方案1】:

输入示例

Root|A
Root|B|C
Root|B|D
Root|E

您的代码的问题是您只处理TreeNodes[0] 元素。要处理元素集合,您需要一个循环

 public MainWindowVM()
{
    private ObservableCollection<Node> mRootNodes;
    public IEnumerable<Node> RootNodes { get { return mRootNodes; } }

    string[] lines = null;
    try
    {
        lines = System.IO.File.ReadAllLines(MainWindow.TextFilePath , System.Text.Encoding.Default);
    }
    catch (IOException ex)
    {
        MessageBox.Show(ex.Message);
        Environment.Exit(0);
    }
    if (lines == null || lines.Length == 0)
    {
        MessageBox.Show("Text file has no content!");
        Environment.Exit(0);
    }
    Dictionary<string, Node> nodeCache = new Dictionary<string, Node>();
    // processing each line
    foreach (var line in lines)
    {                
        Node parentNode = null;
        string key = null;
        // in each line there are one or more node names, separated by | char
        foreach (string childNodeName in line.Split('|'))
        {
            Node childNode;
            // names are not unique, we need a composite key (full node path)
            key += "|" + childNodeName;
            // each node has unique key
            // if key doesn't exists in cache, we need to create new child node
            if (false == nodeCache.TryGetValue(key, out childNode))
            {
                childNode = new Node { Name = childNodeName };
                nodeCache.Add(key, childNode);

                if (parentNode != null)
                    // each node (exept root) has a parent
                    // we need to add a child node to parent ChildRen collection
                    parentNode.Children.Add(childNode);
                else 
                    // root nodes are stored in a separate collection
                    mRootNodes.Add(childNode);
            }

            // saving current node for next iteration
            parentNode = childNode;
        }
    }
}

【讨论】:

  • 工作正常。谢谢
  • 另一件事:应该允许具有相同名称的节点。我如何用字典做到这一点?示例:根 |一个根 |乙| C 根 |乙| D
  • @sanjar14,如果名称不是唯一的,则它不能是键。我为节点制作了一个复合键。查看我的编辑
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-03
  • 2012-02-28
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 2021-12-22
  • 2020-04-16
相关资源
最近更新 更多