views:

165

answers:

2

I have a business object project, which contains composite structure:

public class Tree 
{ public IProductComponent TreeRoot { get; set; } }

public interface ITreeComponent 
{ public string Name { get; set; } }

public class ContainerComponent : ITreeComponent
{ public BindingList<ITreeComponent> Children { get; set; } }

public class LeafComponent : ITreeComponent
{ }

I need to bind this structure to a TreeView in my WPF project. The tree view first:

<TreeView x:Name="treeView" Grid.ColumnSpan="2">
 <TreeView.Resources>
  <HierarchicalDataTemplate 
   ItemsSource="{Binding Children}" 
DataType="{x:Type businessObjects:ContainerComponent}">
   <Label Content="{Binding Name}"/>
  </HierarchicalDataTemplate>
  <DataTemplate DataType="{x:Type businessObjects:LeafComponent}">
   <Label Content="{Binding Name}"/>
  </DataTemplate>
 </TreeView.Resources>
</TreeView>

And the code for binding:

bTreeView = new Binding();
bTreeView.Source = MyTree;
bTreeView.Path = new PropertyPath("TreeRoot.Children");
treeView.SetBinding(TreeView.ItemsSourceProperty, bTreeView);

The problem is that the TReeView does not actually use those templates (it displays only the top level of hierarchy and calls .ToString() to display those items. Please tell me where have I gone wrong. Otherwise, if I set the it is working, but I cannot define two templates there.

Thanks.

A: 

Well I notice you are putting the template in resources, not under TreeVeiw.ItemTemplate.

TreeView should have an ItemTemplate (the Hierarchical) and the ItemsSource set. Shouldn't need anything more than that.

Would help with example data for us to test though.

Chris Ridenour
Just to note - ItemTemplate does not allow defining different templates like Hierarchical and simple DataTemplate at the same time - that is why I have to use resources ;)
Jefim
Is there a specific reason you need both? What is the data you are trying to bind?
Chris Ridenour
A: 

My bad - the Main assembly was loading the dll with entities two times instead of one. That caused it to go crazy - as soon as I fixed it and the assembly loaded once the problems went away.

Jefim