Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your ItemTemplate is trying to render a "Name" and "Age" property in TextBlocks, but TreeViewItem doesn't have an "Age" property and you aren't setting its "Name".</p> <p>Because you're using an ItemTemplate, there's no need to add TreeViewItems to the tree. Instead, add your Person instances directly:</p> <pre><code>_treeView.Items.Add(new Person { Name = "Sally", Age = 28}); </code></pre> <p>The problem, of course, is that your underlying object ("Person") doesn't have any concept of hierarchy, so there's no simple way to add "Joe" to "Sally". There are a couple of more complex options:</p> <p>You could try handling the TreeView.ItemContainerGenerator.StatusChanged event and wait for the "Sally" item to be generated, then get a handle to it and add Joe directly:</p> <pre><code>public Window1() { InitializeComponent(); var bob = new Person { Name = "Bob", Age = 34 }; var sally = new Person { Name = "Sally", Age = 28 }; _treeView.Items.Add(bob); _treeView.Items.Add(sally); _treeView.ItemContainerGenerator.StatusChanged += (sender, e) =&gt; { if (_treeView.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated) return; var sallyItem = _treeView.ItemContainerGenerator.ContainerFromItem(sally) as TreeViewItem; sallyItem.Items.Add(new Person { Name = "Joe", Age = 15 }); }; } </code></pre> <p>Or, a better solution, you could introduce the hierarchy concept into your "Person" object and use a HierarchicalDataTemplate to define the TreeView hierarchy:</p> <p>XAML:</p> <pre><code>&lt;Window x:Class="TreeTest.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WindowTree" Height="300" Width="300"&gt; &lt;Grid&gt; &lt;TreeView Name="_treeView"&gt; &lt;TreeView.ItemTemplate&gt; &lt;HierarchicalDataTemplate ItemsSource="{Binding Subordinates}"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Text="{Binding Path=Name}" /&gt; &lt;TextBlock Text="{Binding Path=Age}" /&gt; &lt;/StackPanel&gt; &lt;/HierarchicalDataTemplate&gt; &lt;/TreeView.ItemTemplate&gt; &lt;/TreeView&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>CODE:</p> <pre><code>using System.Collections.Generic; using System.Windows; namespace TreeTest { /// &lt;summary&gt; /// Interaction logic for Window1.xaml /// &lt;/summary&gt; public partial class Window1 : Window { public Window1() { InitializeComponent(); var bob = new Person { Name = "Bob", Age = 34 }; var sally = new Person { Name = "Sally", Age = 28 }; _treeView.Items.Add(bob); _treeView.Items.Add(sally); sally.Subordinates.Add(new Person { Name = "Joe", Age = 15 }); } } public class Person { public Person() { Subordinates = new List&lt;Person&gt;(); } public string Name { get; set; } public int Age { get; set; } public List&lt;Person&gt; Subordinates { get; private set; } } } </code></pre> <p>This is a more "data-oriented" way to display your hierarchy and a better approach IMHO.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload