2

I'm presenting a tree structure via a TreeView. As it is unknown how deep the tree will be, I'm using a recursive HierarchicalDataTemplate.

In the following code I'm Databinding to an external Object Tree

public class Tree : ObservableCollection<Node> {}

This works fine:

<TreeView ItemsSource="{Binding Source={StaticResource Tree}}">
    <TreeView.Resources>
        <HierarchicalDataTemplate  DataType="{x:Type src:Node}" ItemsSource="{Binding Path=Children}">
            <TextBlock Text="{Binding Path=Name}" />
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

But when I try to bind to a Property

public ObservableCollection<Node> LocalTree{
    get;
    set;
}

Changing the first line of the XAML to

<TreeView ItemsSource="{Binding LocalTree}">

The TreeView stays empty. What am I doing horribly wrong?

Kezaer
  • 53
  • 5
  • Maybe instead of ObservableCollection put Tree in "public ObservableCollection LocalTree" – Jamaxack Feb 20 '15 at 12:41
  • The Tree object was only for testing purposes and has no other usage, i.e. I'm really interested in displaying ObservableCollection; The property LocalTree represents 1-level Children. – Kezaer Feb 20 '15 at 12:45
  • You changed `{Binding Source={StaticResource Tree}}` to `{Binding LocalTree}`. Why not `{Binding Source={StaticResource LocalTree}}` ? May be your binding are wrong? – Yuri Dorokhov Feb 20 '15 at 13:05
  • {Binding Source={StaticResource XXX}} is a way to bind to external Objects. In my Project i have other instances of using a TreeView where I have properties which are bound to directly by ItemsSource="{Binding XXX}" and everything works fine. – Kezaer Feb 20 '15 at 13:10
  • So, if you change type of `LocalTree` from your C# code in question from `ObservableCollection` to `Tree` it works? Unbelievable :) – Yuri Dorokhov Feb 20 '15 at 13:13
  • @Yuri Dorokhov no, you have misunderstood me. I have other situations where I'm binding a TreeView to a Property "public ObservableCollection CollectionOfNodes{}" via ItemsSource="{Binding CollectionOfNodes}" and everything works fine. The Tree class is not needed and will be deleted, was just for testing. – Kezaer Feb 20 '15 at 13:22
  • If you have a working example already, then just compare your current code to that and find the difference(s). It's probably a simple binding or data error... look in your Output Window in Visual Studio to find your error(s). – Sheridan Feb 20 '15 at 13:46
  • That's the problem - even after multiple comparisons I cannot find the error. – Kezaer Feb 23 '15 at 12:25

0 Answers0