2016-10-24 3 views
0

클래스 Node을 사용하여 TreeViewItem을 만듭니다. 예제에서 노드는 소스 코드로 지정됩니다.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

나는 특히 게시물의 생각처럼 * 텍스트 파일 * 내용 * 등의 스크린 샷 *. ** text **로 게시하고 질문에'Node' 클래스의 코드를 포함시킬 수 있습니까 (틀린 링크) – ASh

+0

"노드"상단을 클릭하십시오. 링크를 수정했습니다. – 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