Binding to TreeView in WPF
Posted
by KrisTrip
on Stack Overflow
See other posts from Stack Overflow
or by KrisTrip
Published on 2010-03-24T14:00:54Z
Indexed on
2010/03/24
14:03 UTC
Read the original article
Hit count: 414
I am trying to bind some data from a class instance to a TreeView. My code is as follows:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Parent myClass = new Parent();
this.DataContext = myClass;
}
}
public class Parent
{
public String Name;
public List<string> Children = new List<string>();
private static int count = 0;
public Parent()
{
this.Name = "Test";
for (int i = 0; i < 10; i++)
{
Children.Add(i.ToString());
}
}
}
And the XAML:
<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">
<StackPanel Orientation="Horizontal" VerticalAlignment="Stretch">
<TreeView Name="TreeView" ItemsSource="{Binding}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</StackPanel>
</Window>
Nothing shows up in my TreeView. What am I doing wrong?
© Stack Overflow or respective owner