yu_ominae yu_ominae - 5 months ago 48
C# Question

Indenting top hierarchical items in a wpf treeview

I am trying to provide a separation (margin) between the top hierarchical items in a WPF

. The problem is that I cannot figure out how to write the
for it to apply only to the top items and not to every item.

The code for my
looks like this:

<TreeView ItemContainerStyle="{StaticResource treeViewItemStyle}"
ItemsSource="{Binding Container.RootRules}"
<HierarchicalDataTemplate DataType="{x:Type me:HybridForecastRulesViewModel}"
ItemsSource="{Binding Children}">
<Border Name="bd"

<HierarchicalDataTemplate DataType="{x:Type me:RootRulesViewModel}"
ItemsSource="{Binding Rules}">

I have a style for the treeViewItems like this:

<Style x:Key="treeViewItemStyle"
BasedOn="{StaticResource {x:Type TreeViewItem}}"
TargetType="{x:Type TreeViewItem}">

<Setter Property="Margin" Value="0,10,0,0" />

<DataTrigger Binding="{Binding IsVisible}" Value="False">
<Setter Property="Visibility" Value="Collapsed" />


But this style applies to items of both type (
), when I would like for it to only apply to items of type
. How can this be done?

And the icing on the cake would be for all
items to have a top
of 10, except or the first one.


TreeViewItem has no Level property to use.

So you have 2 options:

1) this. look at the TreeLevelConverter, it's interesting. They bind the control itself and use the converter to retrieve the Level. In your case, you can extend the converter so after it retrieves the Level, converts it in a Thickness instance to use as Margin.

2) you can create a Level property on your ViewModels (possibly in their base class in order to avoid code duplication). Then, every time that you add a child to a ViewModel node, you set the Level on that child. In the xaml, you bind the Margin property to the Level on the ViewModel, using a converter that returns different Thickness depending if the Level is 1 or not.


This is how you set a common Style for all the TreeViewItem:

    <Style TargetType="{x:Type TreeViewItem}">
      <Setter 1 .../>
      <Setter 2 .../>
      <Setter Property="Margin"
              Value="{Binding Level, Converter={StaticResource LevelToMarginConverter}}"/>
    <!-- here your hierarchical DataTemplate... -->
    <HierarchicalDataTemplate ... />