title: WPF Treeview adding sub items from database
body: <p>I have a function that is supposed to return a list of repositories including their subs called LoadAllRepositories.</p>
  <p>This is the typical output from it:</p>
  <pre><code>{
      &quot;id&quot;: &quot;1&quot;,
      &quot;parent&quot;: &quot;#&quot;,
      &quot;text&quot;: &quot;ACC&quot;,
      &quot;TenantId&quot;: &quot;11ff3c48-2b82-41a2-b4f2-ca914e2701c0&quot;,
      &quot;ApplicationId&quot;: &quot;793b0f48-5fea-4e89-8650-602a43099342&quot;,
      &quot;Path&quot;: &quot;DIRONE/ACC&quot;,
      &quot;LinkPath&quot;: null,
      &quot;Applications&quot;: null,
      &quot;CreatedOn&quot;: &quot;2020-09-25T16:25:59.196927&quot;
  },
  {
      &quot;id&quot;: &quot;2&quot;,
      &quot;parent&quot;: &quot;1&quot;,
      &quot;text&quot;: &quot;2020&quot;,
      &quot;TenantId&quot;: &quot;11ff3c48-2b82-41a2-b4f2-ca914e2701c0&quot;,
      &quot;ApplicationId&quot;: &quot;793b0f48-5fea-4e89-8650-602a43099342&quot;,
      &quot;Path&quot;: &quot;DIRONE/ACC/2020&quot;,
      &quot;LinkPath&quot;: null,
      &quot;Applications&quot;: null,
      &quot;CreatedOn&quot;: &quot;2020-09-25T16:26:17.893116&quot;
  },
  {
      &quot;id&quot;: &quot;3&quot;,
      &quot;parent&quot;: &quot;#&quot;,
      &quot;text&quot;: &quot;BCC&quot;,
      &quot;TenantId&quot;: &quot;11ff3c48-2b82-41a2-b4f2-ca914e2701c0&quot;,
      &quot;ApplicationId&quot;: &quot;793b0f48-5fea-4e89-8650-602a43099342&quot;,
      &quot;Path&quot;: &quot;DIRONE/BCC&quot;,
      &quot;LinkPath&quot;: null,
      &quot;Applications&quot;: null,
      &quot;CreatedOn&quot;: &quot;2020-09-25T16:25:59.196927&quot;
  },
  {
      &quot;id&quot;: &quot;4&quot;,
      &quot;parent&quot;: &quot;3&quot;,
      &quot;text&quot;: &quot;2020&quot;,
      &quot;TenantId&quot;: &quot;11ff3c48-2b82-41a2-b4f2-ca914e2701c0&quot;,
      &quot;ApplicationId&quot;: &quot;793b0f48-5fea-4e89-8650-602a43099342&quot;,
      &quot;Path&quot;: &quot;DIRONE/BCC/2020&quot;,
      &quot;LinkPath&quot;: null,
      &quot;Applications&quot;: null,
      &quot;CreatedOn&quot;: &quot;2020-09-25T16:25:59.196927&quot;
  }
  </code></pre>
  <p>if the parent is #, that means it is a root item.</p>
  <p>I am trying to dynamically populate my treeview in wpf using this data schema but so far I have been unable to properly do it. My current code returns a stack overflow error due to it looping to infinity. I am just stuck on how to solve this issue and how to display it on the view.</p>
  <p>Here is the function:</p>
  <pre><code>private List&lt;DocumentRepository&gt; LoadAllRepositories(string ApplicationId, string TenantId)
  {
      List&lt;DocumentRepository&gt; data = new List&lt;DocumentRepository&gt;();
      List&lt;DocumentRepository&gt; initialData = new List&lt;DocumentRepository&gt;();
      var gtl = GetAllRepositories(ApplicationId, TenantId);
      if (gtl == null)
      {
          Error(&quot;Unable to establish a connection to the Server&quot;);
          System.Windows.Application.Current.Shutdown();
          return null;
      }
      foreach (var item in gtl)
      {
          initialData.Add(new DocumentRepository
          {
              Id = item.Id,
              parent = item.parent,
              TenantId = item.TenantId,
              ApplicationId = item.ApplicationId,
              text = item.text,
              Path = item.Path,
              LinkPath = item.LinkPath
          });
      };
      var c = GetChildren(gtl, null, TenantId);
      return data;
  }
  
  private List&lt;DocumentRepository&gt; GetChildren(List&lt;DocumentRepository&gt; Input, string ParentId, string TenantId)
  {
      if (ParentId == null)
      {
          ParentId = &quot;#&quot;;
      }
      List&lt;DocumentRepository&gt; data = new List&lt;DocumentRepository&gt;();
      foreach (DocumentRepository child in Input.Where(x =&gt; x.parent == ParentId &amp;&amp; x.TenantId == TenantId))
      {
          child.Children = LoadAllRepositories(ApplicationId, TenantId);
          data.Add(child);
      }
      return data;
  }
  </code></pre>
  <p>My View:</p>
  <pre><code>&lt;TreeView Name=&quot;DocumentRepositoryTreeview&quot;&gt;
      &lt;TreeView.ItemTemplate&gt;
          &lt;HierarchicalDataTemplate ItemsSource=&quot;{Binding lar}&quot; DataType=&quot;{x:Type model:DocumentRepository}&quot;&gt;
              &lt;StackPanel Orientation=&quot;Horizontal&quot;&gt;
                  &lt;TextBlock Text=&quot;{Binding text}&quot; /&gt;
              &lt;/StackPanel&gt;
          &lt;/HierarchicalDataTemplate&gt;
      &lt;/TreeView.ItemTemplate&gt;
  &lt;/TreeView&gt;
  
  private void Window_Loaded(object sender, RoutedEventArgs e)
  {
      var lar = LoadAllRepositories(ApplicationId, TenantId);
      var dis = new ObservableCollection&lt;DocumentRepository&gt;(lar);
      this.DataContext = dis;
      DocumentRepositoryTreeview.ItemsSource = lar;
  }
  </code></pre>