Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<window>
<window.resources>
<style x:Key="Style1" TargetType="TreeviewItem">
.
.
</Style>

<style x:Key="Style2" TargetType="TreeviewItem">
.
.
</Style>
</window.resources>
<grid>

<treeview x:Name="tree1">
<treeviweitem header="A" />
  <treeviweitem header="A1" />
  <treeviweitem header="A2" />
<treeviweitem header="B" />
</treeView>

<treeview x:Name="tree2"/>
<treeviewitem header="C" />
  <treeviewitem header="C1" />
  <treeviewitem header="C2" />
<treeviewitem header="D" />
</grid>
</window>



I have a question, hope to get your help please!
my question is

1、how to set that all of treeviewitem of "tree1"(include "A" "A1" "A2" "B") to apply the style "style1"

2、how to set that all of treeviewitem of "tree2"(include "C" "C1" "C2" "D") to apply the style "style2"


I know can do that set each of treeviewitem style like this

XML
<treeviewitem header="C" Style="{Staticresource style2}"/>
  <treeviewitem header="C1" Style="{Staticresource style2}"/>
  <treeviewitem header="C2" Style="{Staticresource style2}"/>
<treeviewitem header="D" Style="{Staticresource style2}"/>


, but this is too stupid(this need set each of listviewitem)
Posted

1 solution

Hello,

You can embed a default style in TreeView's private resource dictionary, or set TreeView's ItemContainerStyle.

<TreeView>
   <Treeview.resources>
     <Style Targettype="{x:Type TreeViewItem}">
            BasedOn="{StaticResource {x:Type TreeViewItem}}">
       <Setter Property="Background" Value="Green" />
     </Style>
   </Treeview.resources>
   <Treeviewitem header="Merry" />
   <Treeviewitem header="Christmas" />
</Treeview> 


XML
<Grid.Resources>
   <ResourceDictionary>
     <Style x:Key="style" TargetType="{x:Type TreeViewItem}"
            BasedOn="{StaticResource {x:Type TreeViewItem}}">
            <Setter Property="Background" Value="Green" />            
            <Setter Property="ItemContainerStyle" Value="{Binding ItemContainerStyle, RelativeSource={RelativeSource AncestorType={x:Type TreeView}}}" />
     </Style>            
   </ResourceDictionary>
 </Grid.Resources>
  
 <TreeView ItemContainerStyle="{StaticResource style}">
   <TreeViewItem Header="Merry" >
     <TreeViewItem Header="Christmas" />
   </TreeViewItem>
 </TreeView>


Regards
Joseph Leung
 
Share this answer
 
v3
Comments
gumaolin 26-Nov-13 9:00am    
Thank you very much for your answer, I follow your method a try, the first method can meet the requirements, second methods (ItemContainerStyle) can only be applied to the level 0 node


thank again
Leung Yat Chun 26-Nov-13 9:58am    
Hello, you can bind child's ItemContainerStyle to parent's ItemContainerStyle.

If you need different style for different TreeViewItem (e.g. alternate background color), you have to use ItemContainerStyleSelector instead of ItemContainerStyle.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900