views:

268

answers:

1

Hi!

I've a problem building up a ComponentOne TreeView in Silverlight (C1TreeView) with a C1HierarchicalDataTemplate. In detail the Tree only shows 2 levels (H1 and H2), although 3 levels are defined through HierarchicalDataTemplates like:

<c1:C1HierarchicalDataTemplate x:Key="H3Template">
          <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" />
     </c1:C1HierarchicalDataTemplate>

    <c1:C1HierarchicalDataTemplate x:Key="H2Template" ItemsSource="{Binding Path=H3Items}" ItemTemplate="{StaticResource H3Template}">
        <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" />
    </c1:C1HierarchicalDataTemplate>

    <c1:C1HierarchicalDataTemplate x:Key="H1Template" ItemsSource="{Binding Path=H2Items}" ItemTemplate="{StaticResource H2Template}">
        <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" />
    </c1:C1HierarchicalDataTemplate>");

I'm using this Templates in a Custom TreeView (derived from C1TreeView):

 <c1:C1TreeView ... ItemTemplate="{StaticResource H1Template}">
 </c1:C1TreeView>

The constructor of this TreeView looks like this:

public MyTreeView(ObservableCollection<H1> h1Items)
{
    InitializeComponent();
    ItemsSource = h1Items;
}

Can anybody see the error in these code snippets??

thx, Dom

A: 

While I'm unfamiliar with the ComponentOne TreeView that you're using, and despite the fact that you are using Silverlight, normally in WPF when you are using HierarchicalDataTemplates, you tell the template what type it's for. Sub-item templates are similarly told what type they apply to. You don't specifically tell the data template what template to use for it's ItemTemplate. That is automatically figured out by the system based on the type of object. This also applies when you bind an item collection to the TreeView--you don't have to specify the ItemTemplate.

So in your case (local: is a namespace defined at the top of your xaml):

<c1:C1HierarchicalDataTemplate DataType="{x:Type local:H1}" 
                               ItemsSource="{Binding Path=H2Items}">
  <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" />
</c1:C1HierarchicalDataTemplate>

<c1:C1HierarchicalDataTemplate DataType="{x:Type local:H2}" 
                               ItemsSource="{Binding Path=H3Items}">
  <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" />
</c1:C1HierarchicalDataTemplate>

<c1:C1HierarchicalDataTemplate DataType="{x:Type local:H3}">
  <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" />
</c1:C1HierarchicalDataTemplate>

And the TreeView:

<c1:C1TreeView ItemsSource="{Binding SomeH1List}"/>

Of course, as I said, this applies to WPF, so it might not apply in your case.

Benny Jobigan