title: Adding Custom Class as TreeViewItem to TreeView WPF
body: <p>I have scenario where my TreeView look like below:</p>
  
  <ul>
  <li>MainParent</li>
  <li>.>-Parent</li>
  <li>...>--Child1</li>
  <li>...>--Child2</li>
  <li>...>--Child3</li>
  <li>...>--Child4</li>
  <li>...>--Child5</li>
  <li>......> Child1_Of_Child5</li>
  <li>...........>Child1(Child1_Of_Child5)</li>
  <li>...........>Child2(Child1_Of_Child5)</li>
  <li>......> Child2_of_Child5</li>
  <li>...........>Child1(Child2_Of_Child5)</li>
  <li>...........>Child2(Child2_Of_Child5)</li>
  </ul>
  
  <p>I am able to bind ObservableCollection till Child1/Child2/../Child5 using HierarchicalDataTemplate.
  Though I am able to add Child1_Of_Child5 to Child5 ObservableCollection (seen in debug mode), Same is not reflecting in UI/Treeview.</p>
  
  <p>Below is my XAML code:</p>
  
  <pre><code>&lt;TreeView.ItemTemplate&gt;
                      &lt;HierarchicalDataTemplate DataType="{x:Type VM:ProjectViewModel}" ItemsSource="{Binding SelectActivityViewModelCollection}"&gt;
                          &lt;StackPanel Orientation="Horizontal"&gt;
                              &lt;Image Source="./resources/New_Package.png" Width="15" Height="15"/&gt;
                              &lt;TextBlock Text="{Binding Header}"&gt;&lt;/TextBlock&gt;
                          &lt;/StackPanel&gt;
                          &lt;HierarchicalDataTemplate.ItemTemplate&gt;
                              &lt;HierarchicalDataTemplate DataType="{x:Type VM:SelectActivityViewModel}" ItemsSource="{Binding ToolsViewModelCollection}"
                                       &gt;
                                  &lt;StackPanel Orientation="Horizontal"&gt;
                                      &lt;Image Source="./resources/New_Package.png" Width="15" Height="15"/&gt;
                                      &lt;TextBlock Text="{Binding Header}"&gt;&lt;/TextBlock&gt;
                                  &lt;/StackPanel&gt;
                                  &lt;HierarchicalDataTemplate.ItemTemplate&gt;
                                      &lt;HierarchicalDataTemplate DataType="{x:Type VM:ToolsViewModel}" ItemsSource="{Binding SheetsViewModelBaseCollection}"&gt;
                                          &lt;StackPanel Orientation="Horizontal"&gt;
                                              &lt;Image Source="./resources/New_Package.png" Width="15" Height="15"/&gt;
                                              &lt;TextBlock Text="{Binding Header}"&gt;&lt;/TextBlock&gt;
                                          &lt;/StackPanel&gt;
                                          &lt;HierarchicalDataTemplate.ItemTemplate&gt;
                                              &lt;HierarchicalDataTemplate x:Name="LastLevel" DataType="{x:Type VM:ViewModelBase}" ItemsSource="{Binding FeaturesViewModelCollection}"&gt;
                                                  &lt;StackPanel Orientation="Horizontal"&gt;
                                                      &lt;Image Source="./resources/file.png" Width="15" Height="15"/&gt;
                                                      &lt;TextBlock Text="{Binding Header}"/&gt;
                                                  &lt;/StackPanel&gt;
                                              &lt;/HierarchicalDataTemplate&gt;
                                          &lt;/HierarchicalDataTemplate.ItemTemplate&gt;
  
                                      &lt;/HierarchicalDataTemplate&gt;
                                  &lt;/HierarchicalDataTemplate.ItemTemplate&gt;
                              &lt;/HierarchicalDataTemplate&gt;
                          &lt;/HierarchicalDataTemplate.ItemTemplate&gt;
                      &lt;/HierarchicalDataTemplate&gt;
                  &lt;/TreeView.ItemTemplate&gt;
  </code></pre>
  
  <p>Class Structure:
  (ViewModelBase is extended to all classes)</p>
  
  <ul>
  <li>ProjectViewModel - has SelectActivityViewModelCollection = (ObservableCollection(SelectActivityViewModel))</li>
  <li>SelectActivityViewModel - has ToolsViewModelCollection = (OC(ToolsViewModel))</li>
  <li>ToolsViewModel - has SheetsViewModelBaseCollection and FeaturesViewModelCollection = (OC(ViewModelBase) and OC(FeaturesViewModel))</li>
  <li>FeaturesViewModel - has UseCaseViewModelCollection = (OC(UseCaseViewModel))</li>
  <li>In ToolsViewModel I am using SheetsViewModelBaseCollection to store Child1/Child2/.../Child5</li>
  <li>and FeaturesViewModelCollection - to store Child1_Of_Child5/Child2_Of_Child5/../Child(n)_Of_Child5</li>
  <li><p>EDIT : As i have extended ViewModelBase to all classes, i am using OC(ViewModelBase) to store different sheets(.i.e.,in SheetsViewModelCollection).
  Note: </p>
  
  <ol>
  <li>No DataBase is used: Serialisation and Deserialisation is used</li>
  <li>WPF : MVVM pattern is followed</li>
  </ol></li>
  </ul>
  
  <p><a href="https://i.stack.imgur.com/sglwr.png" rel="nofollow noreferrer">Image shows complete TreeView and OC of respective classes</a></p>