title: WPF TreeView with HierarchicalDataTemplate and ObservableCollection containing Children
body: <p>I'm trying to get this to work but unfortunately I have no idea what I'm doing wrong! I've looked into every uestion asked so far containing TreeView and the HierarchicalDataTemplate.</p>
  
  <p>Here's the problem: I have a TreeView and would like to show the folder structure with all SubFolders and Files in it. To do so I created a class with the necessary Items:</p>
  
  <pre><code>public class FolderItem : INotifyPropertyChanged
  {
      public event PropertyChangedEventHandler PropertyChanged;
  
      public void OnPropertyChanged(string propertyName)
      {
          var handler = PropertyChanged;
  
          if (handler != null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          }
      }
      public FolderItem()
      {
          Files = new ObservableCollection&lt;FileItem&gt;();
          SubFolders = new ObservableCollection&lt;FolderItem&gt;();
      }
  
      #region:PrivateVariables
      private DirectoryInfo _Info;
      private ObservableCollection&lt;FileItem&gt; _Files;
      private ObservableCollection&lt;FolderItem&gt; _SubFolders;
      #endregion
  
      public DirectoryInfo Info
      {
          get { return _Info; }
          set
          {
              _Info = value;
              OnPropertyChanged("Info");
          }
      }
      public ObservableCollection&lt;FileItem&gt; Files
      {
          get { return _Files; }
          set
          {
              _Files = value;
              OnPropertyChanged("Files");
          }
      }
      public ObservableCollection&lt;FolderItem&gt; SubFolders
      {
          get { return _SubFolders; }
          set
          {
              _SubFolders = value;
              OnPropertyChanged("SubFolders");
          }
      }
  }
  public class FileItem : INotifyPropertyChanged
  {
      public event PropertyChangedEventHandler PropertyChanged;
  
      public void OnPropertyChanged(string propertyName)
      {
          var handler = PropertyChanged;
  
          if (handler != null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          }
      }
  
      #region:PrivateVariables
      private FileInfo _Info;
      #endregion
  
      public FileInfo Info
      {
          get { return _Info; }
          set
          {
              _Info = value;
              OnPropertyChanged("Info");
          }
      }
  }
  </code></pre>
  
  <p>In the code I now loop through all Folders and Files and the ObservableCollection folders now shows the correct data!  The code I use looks like this:</p>
  
  <pre><code>public void StartExtraction(string sPath)
  {
      if (Directory.Exists(sPath))
      {
          FolderItem newFolder = new FolderItem();
          newFolder.Info = new DirectoryInfo(sPath);
  
          GetFileCount(sPath, newFolder);
  
          Application.Current.Dispatcher.Invoke((Action)delegate ()
          {
              ViewModel_ZVLB.folders.Add(newFolder);
          });
      }
  }
  
  public void GetFileCount(string sPath, FolderItem actualFolder)
  {
      if (Directory.Exists(sPath))
      {
          foreach (string fileName in Directory.GetFiles(sPath))
          {
              FileItem newFile = new FileItem();
              newFile.Info = new FileInfo(fileName);
              actualFolder.Files.Add(newFile);
          }
  
          foreach (string subFolder in Directory.GetDirectories(sPath))
          {
              FolderItem newSubFolder = new FolderItem();
              newSubFolder.Info = new DirectoryInfo(subFolder);
              actualFolder.SubFolders.Add(newSubFolder);
  
              GetFileCount(subFolder, newSubFolder);
          }
      }
  }
  </code></pre>
  
  <p>With this OC I go to the XAML and tried a lot to show the Data:</p>
  
  <pre><code>    &lt;TreeView ItemsSource="{Binding folders}"&gt;
      &lt;TreeView.Resources&gt;
          &lt;HierarchicalDataTemplate ItemsSource="{Binding folders}" DataType="{x:Type local:FolderItem}"&gt;
              &lt;HierarchicalDataTemplate.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;TextBlock Text="{Binding Info.Name}" /&gt;
                  &lt;/DataTemplate&gt;
              &lt;/HierarchicalDataTemplate.ItemTemplate&gt;
          &lt;/HierarchicalDataTemplate&gt;
      &lt;/TreeView.Resources&gt;
  
      &lt;TreeView.ItemContainerStyle&gt;
          &lt;Style TargetType="{x:Type TreeViewItem}"&gt;
              &lt;Setter Property="HorizontalContentAlignment" Value="Stretch"/&gt;
          &lt;/Style&gt;
      &lt;/TreeView.ItemContainerStyle&gt;
  
      &lt;TreeView.ItemTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;TextBlock Text="{Binding Info.FullName}" FontSize="16" FontWeight="Bold" /&gt;
          &lt;/DataTemplate&gt;
      &lt;/TreeView.ItemTemplate&gt;
  &lt;/TreeView&gt;
  </code></pre>
  
  <p>Even with another HierarchicalDataTemplate it won't work.</p>
  
  <p>Is there anything I did wrong? Or is it just not working with ObservableCollections?</p>
  
  <p>Intereisting is also that after the Update of Visual Studio 2017 a new Error appeared: As "folders" for Tpye "ViewModel_ZVLB" a instancemenber is expected! (translated from German)
  Has this something to do with my problem?</p>
  
  <p>Thanks for your help!</p>