title: WPF: Tree view by parsing string
body: <p>Why is my tree displaying only up to 2 levels? For example, <code>NetworkControl.AddressData.MessageOriginatorID.Value</code> shows only <code>NetworkControl</code> as root and <code>AddressData</code> as a child but <code>AddressData</code> doesn't show <code>MessageOriginatorID.Value</code> as a child.</p>
  
  <p>Input data: </p>
  
  <pre><code>       public List&lt;TreeModel&gt; GetRequestTreeNodes()
          {
  
          string[] nodes = {"NetworkControl.AlternateIndexText.Value",
                               "NetworkControl.AddressData.DestinationID",
                               "NetworkControl.AddressData.MessageOriginatorID.Value",
                               "NetworkControl.AddressData.TransactionOriginatorID.Value",
                               "NetworkControl.AddressData.MessageOriginatorID.Value",
                               "VehicleIdentification.IdentificationID.Value",
                               "VehicleSummary.VehicleIdentification.IdentificationID.Value",
                               "TitleSummary.TitleIdentification.IdentificationID.Value",
                               "TitleSummary.JurisdictionTitlingKeyText.Value",
                               "VehicleSummary.VehicleIdentification.IdentificationID.Value",
                                };
  
          List&lt;TreeModel&gt; nodeList = BuildTree(nodes);
  
          return nodeList;
  
      }
  </code></pre>
  
  <p>Parser method:</p>
  
  <pre><code>      public List&lt;TreeModel&gt; BuildTree(IEnumerable&lt;string&gt; strings)
      {
          return (
            from s in strings
            let split = s.Split('.')
            group s by s.Split('.')[0] into g
            select new TreeModel
            {
                Name = g.Key,
                Children = BuildTree(
                  from s in g
                  where s.Length &gt; g.Key.Length + 1
                  select s.Substring(g.Key.Length + 1))
            }
            ).ToList();
  
  
      }       
  </code></pre>
  
  <p>Method that populates the data to treeview:</p>
  
  <pre><code>     public List&lt;TreeViewModel&gt; GetRequestTreeNodesFromModel()
         {
          treeNodeViewModel = treeModel.GetRequestTreeNodes().Select(a =&gt; new TreeViewModel
          {
              Children = a.Children.Select(c =&gt; new TreeViewModel { Name = c.Name }).ToList(),
              Name = a.Name,
  
          }).ToList();
  
          return treeNodeViewModel;
      }
  </code></pre>
  
  <p>XAML</p>
  
  <pre><code>     &lt;TreeView Margin="644,137,6,6" Grid.RowSpan="2" ItemsSource="{Binding Path=TreeView}"&gt;
              &lt;TreeView.Resources&gt;
                  &lt;HierarchicalDataTemplate DataType="{x:Type local:TreeViewModel}" ItemsSource="{Binding Path= Children}"&gt;
                          &lt;CheckBox IsChecked="{Binding Name}" Content="{Binding Name}" /&gt;
                      &lt;!--&lt;TextBlock Text="{Binding Name}" /&gt;--&gt;
                  &lt;/HierarchicalDataTemplate&gt;
                  &lt;/TreeView.Resources&gt;
          &lt;/TreeView&gt;
  </code></pre>