views:

57

answers:

1

I am trying to bind recursively to the children of an item in a TreeView. From what I can see on MSDN HierarchicalDataTemplate is the way to go, but thus far I've only been partially successful.

My class:

 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DocumentText test = new DocumentText();
        this.DataContext = test;

        for (int i = 1; i < 5; i++)
        {
            test.AddChild();
        }
        foreach (DocumentText t in test.Children)
        {
            t.AddChild();
            t.AddChild();
        }
    }
}

partial class DocumentText
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public override string ToString()
    {
        return Name;
    }

    public List<DocumentText> _children;
    public List<DocumentText> Children
    {
        get { return this._children; }
    }

    public DocumentText()
    {
        _name = "Test";
        _children = new List<DocumentText>();
    }

    public void AddChild()
    {
        _children.Add(new DocumentText());
    }
}

My XAML: In mainview.xaml:

    <Window x:Class="treetest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TreeView Name="binderPanel" DockPanel.Dock="Left" 
                      MinWidth="150" MaxWidth="250" Background="LightGray"
                      ItemsSource="{Binding Children}">
        </TreeView>
    </Grid>
</Window>

In app.xaml:

    <HierarchicalDataTemplate x:Key="BinderTemplate"
 DataType="{x:Type src:DocumentText}" ItemsSource="{Binding Path=/Children}">
            <TreeViewItem Header="{Binding}"/>
        </HierarchicalDataTemplate>

This code produces a list of the first children, but the nested children are not displayed.

+2  A: 

The primary problem in what you posted is that you haven't connected the HierarchicalDataTemplate as the TreeView's ItemTemplate. You need to either set ItemTemplate="{StaticResource BinderTemplate}" or remove the x:Key to apply the template to all DocumentText instances. You should also change the TreeViewItem in the template to a TextBlock - the TreeViewItem is generated for you and what you put in that template is applied to it as a HeaderTemplate.

John Bowen
Thanks! That did the trick. I didn't know about the HeaderTemplate either.
Clay
I'm surprised that your solution works, since your objects don't raise property- or collection-changed events and you're binding to them before you populate their collections.
Robert Rossney
@Robert - I think it's being saved by the fact that Binding doesn't start until after the constructor is completed rather than during InitializeComponent. +1 on the need for change notifications though. It'll save a lot of frustration later if you implement INotifyPropertyChanged and use ObservableCollection instead of List.
John Bowen
Ah, right, of course that's what's going on. Thanks.
Robert Rossney