2016-10-24 105 views
0

我使用類Node創建我的TreeViewItems。在這個例子中,節點是在源代碼中指定的。但我怎麼做,如果節點是從一個文本文件導入的內容是這樣的:C#WPF:從文本文件創建TreeView

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

+1

我特別喜歡發帖的想法*文本文件*內容*截圖*。你可以把它發佈爲**文本**,並且還包括問題中的Node類的代碼(鏈接不正確) – ASh

+0

請點擊「Node」上的頂部。 我已更正鏈接。 – sanjar14

回答

0

輸入例如

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; 
     } 
    } 
} 
+0

工作正常。謝謝 – sanjar14

+0

另一件事:應該允許具有相同名稱的節點。我如何用字典來做到這一點?例如: Root | A Root | B | C Root | B | D – sanjar14

+0

@ sanjar14,如果名稱不唯一,那麼它不能是一個鍵。我爲節點做了一個複合鍵。看到我的編輯 – ASh