2016-10-24 13 views
0

クラスNodeでTreeViewItemを作成します。この例では、ノードはソースコードで指定されています。C#WPF:テキストファイルからツリービューを作成

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

しかし、私は最初の2つのノードだけを取得します。私はどのようにループ全体TreeViewを構築するか分からない。

TreeView

+1

ループを必要としています。あなたは** text **として投稿して頂けますし、質問に '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

ありがとうございます別のこと:同じ名前のノードは許可する必要があります。どのように私は辞書でこれを行うのですか? 例: ルート| A ルート| B | C ルート| B | D – sanjar14

+0

@ sanjar14、名前が一意でない場合は、キーにすることはできません。私はノード用の複合キーを作った。私の編集 – ASh

関連する問題