title: How to create Dynamic TreeView with Parent/Child from database WPF
body: <p>I am trying to create a Dynamic Treeview with the following code. I am using User Control</p>
  
  <p>On Control Load</p>
  
  <pre><code>TreeViewItem treeviewItems = new TreeViewItem();
  treeviewItems.ItemsSource = TreeViewDataSource.DefaultView;
  treeviewItems.ItemTemplate = GetHierarchicalData(ID, Desc);
  treeViewCntrl.Items.Add(treeviewItems);
  
  public HierarchicalDataTemplate GetHierarchicalData(string id, string desc)
  {
     HierarchicalDataTemplate hierdatatemp = null;
     try
     {
      hierdatatemp = new HierarchicalDataTemplate(typeof(DataTable));
      hierdatatemp.ItemsSource = new Binding(itemSourceBindingName);
      FrameworkElementFactory textBlock = new FrameworkElementFactory(typeof(TextBlock));
      textBlock.SetBinding(TextBlock.TextProperty, new Binding(desc));
      hierdatatemp.VisualTree = textBlock;
      }
      catch (Exception ex)
      {  }
      return hierdatatemp;
   }
  </code></pre>
  
  <p>With this code I am able to add an item but it is not adding properly. First it is adding an empty node under and the items are getting added after that.</p>
  
  <p>What I want is items should add without adding any empty node, and on clicking on the Parent node, Child node should get added.</p>
  
  <p>How to add a child node into parent node?</p>