Apply style to all TreeViewItem
Asked Answered
C

1

6

Hi The problem am having is that I have multiple TreeView control and each TreeView has its own TreeViewItem styles, setting it

TreeView ItemContainerStyle="{StaticResource Style1}"

will only set the root element NOT all the child elements, How to apply a style to all the child elements in a TreeView

Causal answered 2/9, 2009 at 9:8 Comment(0)
K
19

There are several ways to accomplish this:

You could make your style the default for all TreeViewItems:

<Style TargetType="{x:Type TreeViewItem}">
...
</Style>

The difference is that you do not set the x:Key attribute, but you do set the TargetType attribute. You do not need to set the ItemContainerStyle on your TreeView in this case.


You could also set your style as the default style for all TreeViewItems, but only within your TreeView:
<TreeView>
    <TreeView.Resources>
        <Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource Style1}"/>
    </TreeView.Resources>
</TreeView>

In this case you also do not need to set the ItemContainerStyle on your TreeView.


You could also alter your style as follows

<Style x:Key="Style1" TargetType="{x:Type TreeViewItem}">
    <Setter Property="ItemContainerStyle" Value="{StaticResource Style1}"/>
</Style>

In this case you'd still have to set the ItemContainerStyle on your TreeView.

Kemme answered 2/9, 2009 at 9:18 Comment(2)
I think it worth to mention about DataTemplates. Who knows, maybe Chandimal wants to change content of TreeViewItem, not the container style, but doesn't realize this... Just a thought :).Abisha
Thanks!, This is what I was looking for!. Its about changing the background /selection color etc.Causal

© 2022 - 2024 — McMap. All rights reserved.