Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding to TreeView in WPF
    text
    copied!<p>I am trying to bind some data from a class instance to a TreeView. My code is as follows:</p> <pre><code>public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Parent myClass = new Parent(); this.DataContext = myClass; } } public class Parent { private List&lt;string&gt; children = new List&lt;string&gt;; public string Name {get;set;} public List&lt;string&gt; Children { get { return this.children; } set { this.children=value; } } public Parent() { this.Name = "Test"; for (int i = 1; i &lt;= 10; i++) { Children.Add(i.ToString()); } } } </code></pre> <p>And the XAML:</p> <pre><code>&lt;Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:loc="clr-namespace:WpfApplication1" Title="MainWindow" Height="287" Width="525"&gt; &lt;StackPanel Orientation="Horizontal" VerticalAlignment="Stretch"&gt; &lt;TreeView Name="TreeView" ItemsSource="{Binding}"&gt; &lt;TreeView.ItemTemplate&gt; &lt;HierarchicalDataTemplate ItemsSource="{Binding Children}"&gt; &lt;TextBlock Text="{Binding Name}"/&gt; &lt;/HierarchicalDataTemplate&gt; &lt;/TreeView.ItemTemplate&gt; &lt;/TreeView&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p>Nothing shows up in my TreeView. What am I doing wrong?</p> <p>I expect to see:</p> <p>-Test<br /> -- 1<br /> -- 2<br /> -- 3<br /> etc<br /></p> <p>updated: to make Name and Children properties instead of public fields.<br /> updated: added what I expect to see in TreeView</p>
 

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