title: Avalonia Dynamic Tree View MVVM
body: <p>In Avalonia,
  I want to create a file tree view.
  When I write</p>
  <pre><code>      &lt;TreeView Height=&quot;500&quot; Background=&quot;Red&quot; &gt;
          &lt;TreeViewItem Header=&quot;h0&quot;&gt;
            &lt;TreeViewItem Header=&quot;h11&quot;&gt;&lt;/TreeViewItem&gt;
  
          &lt;/TreeViewItem&gt;
          &lt;TreeViewItem Header=&quot;h1&quot;&gt;&lt;/TreeViewItem&gt;
  
        &lt;/TreeView&gt;
  </code></pre>
  <p>It works, but obvisously, it's static.</p>
  <p>I tried</p>
  <pre><code>    &lt;TreeView Height=&quot;500&quot; Background=&quot;Red&quot; Items=&quot;{Binding Files}&quot;&gt;
        &lt;TreeView.ItemTemplate&gt;
          &lt;TreeDataTemplate ItemsSource=&quot;{Binding Items}&quot;&gt;
            &lt;TextBlock Text=&quot;{Binding Header}&quot;/&gt;
          &lt;/TreeDataTemplate&gt;
        &lt;/TreeView.ItemTemplate&gt;
      &lt;/TreeView&gt;
  </code></pre>
  <p>with the ViewModel:</p>
  <pre><code>public class RepoFileViewModel : ReactiveObject, IRoutableViewModel
  {
      public string? UrlPathSegment { get; } = Guid.NewGuid().ToString();
  
      public IScreen HostScreen { get; }
  
      public RepoFileViewModel(IScreen screen,)
      {
          HostScreen = screen;
          
          TreeViewItem Root = new TreeViewItem();
  
          TreeViewItem l1a = new TreeViewItem { Header = &quot;level 1 a&quot; };
          TreeViewItem l1b = new TreeViewItem { Header = &quot;level 1 b&quot; };
          TreeViewItem l1c = new TreeViewItem { Header = &quot;level 1 c&quot; };
          Root.Items = new List&lt;TreeViewItem&gt;
          {
              {l1a },{l1b},{l1c }
          };
          TreeViewItem l2ba = new TreeViewItem { Name = &quot;level 2 b a&quot; };
          l1b.Items = new List&lt;TreeViewItem&gt;
          {
              { l2ba }
          };
  
          Files = Root;
  
          this.RaisePropertyChanged(&quot;Files&quot;);
  
  
      }
  
      private TreeViewItem files;
      public TreeViewItem Files
      {
          get =&gt; files;
          set
          {
              this.RaiseAndSetIfChanged(ref files, value);
          }
      }
  
  
  }
  
  </code></pre>
  <p>It does not work.</p>
  <p>How to properly create a dynamic TreeView using MVVM and XAML in Avalonia?</p>