tags:

views:

578

answers:

3
+1  A: 

You can set DataTrigger for the HierarchicalDataTemplate and have different property binding. Just an example below. Please check this earlier answer in this thread just in case you need more ideas.

 <HierarchicalDataTemplate  DataType="{x:Type local:Person}" ItemsSource="{Binding People}" >
    <Grid>
      <TextBlock x:Name="fName" Text="{Binding FirstName}"/>
      <TextBlock x:Name="lName" Text="{Binding LastName}" Visibility="Collapsed"/>
   </Grid>
   <HierarchicalDataTemplate.Triggers> 
    <DataTrigger Binding="{Binding State}" Value="A">
      <Setter TargetName="fName" Property="Visibility" Value="Collapsed"/>
       <Setter TargetName="lName" Property="Visibility" Value="Visible"/>
    </DataTrigger>
  </HierarchicalDataTemplate.Triggers> 
</HierarchicalDataTemplate>

Jobi Joy

Jobi Joy
That won't work, because Hierarchical DataTemplate Only accepts on child right?
Kevin
Oops put a Grid around it..
Jobi Joy
A: 

If your parent and child are different object types, there is a very easy answer: Just use multiple HierarchicalDataTemplates in a ResourceDictionary:

<TreeView ItemsSource="{Binding Parents}">

  <TreeView.ResourceDictionary>

    <HierarchicalDataTemplate
        TargetType="{x:Type my:ParentType}"
        ItemsSource="{Binding Children}">

      ... parent content ...

    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate
        TargetType="{x:Type my:ChildType}"
        ItemsSource="{Binding Children}">

      ... child content ...

    </HierarchicalDataTemplate>

  </TreeView.ResourceDictionary>
</TreeView>

This technique does not work in all scenarios, but when it does it is very powerful and expressive.

Another variation on this if the Parent and Child are the same type but with a different parameter is to create an ItemTemplateSelector that calls LoadResource() to load the appropriate named HierarchicalDataTemplate depending on the data values.

Ray Burns
+1  A: 

to Ray Burns, how are you suppose ot display the child content and the parent content?!

Kevin