4

I try do display hierarchical data with a TreeView and I would like to set different DataTemplates for my different Children types.

But the thing is, that my style does not get applied.

Maybe its a very simple mistake but i really do not find it.

<TreeView ItemsSource="{Binding List}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:Main}" ItemsSource="{Binding Children}">
            <TextBlock Text="{Binding Property1}"/>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type local:Type2}">
            <HierarchicalDataTemplate.ItemContainerStyle>
                <Style TargetType="TreeViewItem">
                    <Setter Property="IsExpanded" Value="True"/>
                </Style>
            </HierarchicalDataTemplate.ItemContainerStyle>
            <TextBlock Text="{Binding Property2}"/>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type local:Type3}">
            <HierarchicalDataTemplate.ItemContainerStyle>
                <Style TargetType="TreeViewItem">
                    <Setter Property="IsExpanded" Value="False"/>
                </Style>
            </HierarchicalDataTemplate.ItemContainerStyle>
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>
MisterPresident
  • 563
  • 7
  • 20
  • First thought - should TargetType not be "{x:Type TreeViewItem}" – user3690202 Aug 24 '15 at 09:08
  • Second thought - there is a typo in "IsExpaned" - it should be "IsExpanded" - notice the second D – user3690202 Aug 24 '15 at 09:09
  • Thank you for answering, but both does not help. – MisterPresident Aug 24 '15 at 09:43
  • Hmm, it's an interesting qn and I'll look into it more. As a workaround, you could set up a style for all TreeViewItems in this tree, bind IsExpanded to some variable on the VM, and then just have it set to false for Type3 and set to true for Type2? That would definitely work and get you to what you want. It just doesn't use the ItemContainerStyle feature. – user3690202 Aug 24 '15 at 09:58

2 Answers2

5

Ok, I know what is going wrong. HierarchicalDataTemplate.ItemContainerStyle contains a style which is applied to the ItemsContainer where the children for the current node are stored. Try this as an experiment, change your style to:

    <HierarchicalDataTemplate.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsExpanded" Value="True" />
            <Setter Property="Foreground" Value="Navy" />
        </Style>
    </HierarchicalDataTemplate.ItemContainerStyle>

You will notice that the node that you put this style onto continues to have a black foreground, but all it's children will now have a foreground of Navy.

It is a little counter-intuitive, but when you think about it, I guess it makes sense. So, bearing this in mind, I think the best solution is to bind IsExpanded for all TreeViewItems to a variable in the VM and then pick different values based on types there.

user3690202
  • 3,844
  • 3
  • 22
  • 36
0

I had a similar issue maybe. In case Main, Type2 and Type3 are interfaces the selection in XAML won't work, I had to use classes. If you want to use interfaces you could implement a template selector.

blueprint
  • 198
  • 8