To render a Border
around the collection of children for a TreeViewItem
we need to modify the Style
for ItemContainerStyle
of the TreeView
TreeViewItem
Style
by default uses a <ItemsPresenter x:Name="ItemsHost" />
to render it's children's content.
Children's Content in the default ItemContainerStyle
is given by
<ItemsPresenter x:Name="ItemsHost"
Grid.Row="1"
Grid.Column="1"
Grid.ColumnSpan="2" />
Now to test this I had a Collection with a bool named Type
and just tried to render a Border
when this bool was True
So I updated the ItemsPresenter
to
<Border Grid.Row="1"
Grid.Column="1"
Grid.ColumnSpan="2"
BorderThickness="1">
<Border.Style>
<Style TargetType="{x:Type Border}">
<Setter Property="BorderBrush"
Value="Transparent" />
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type TreeViewItem}},
Path=DataContext.Type}"
Value="True">
<Setter Property="BorderBrush"
Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<ItemsPresenter x:Name="ItemsHost" />
</Border>
Which then rendered the following
You'll of course have to update the above Bindings to be based on your own cases of when you want the Border
rendered.
In my case my Type
variable was set to True for the Item with "1.1" as it's Header Content.