The effect is as follows:
When we want to deeply define the appearance of a control, we usually define the template of the control. However, the template definition of treeviewitem is complicated (msdn ), because you not only need to define the data presentation of each treeviewitem, but also need to redefine the folding/expansion control of the child members of the treeviewitem. Unless you want to completely change the entire Treeview (like this), if you want to continue to use the default Treeview folding style of the system and only want to change the appearance of the treeviewitem data, you can do so.
We will first demonstrate a Treeview that directly defines the treeviewitem, as follows:
<Treeview>
<Treeviewitem header = "Visual Studio" isexpanded = "true">
<Treeviewitem header = "Visual Studio 2012"/>
<Treeviewitem header = "Visual Studio 2010"/>
</Treeviewitem>
<Treeviewitem header = "sharpdevelop"/>
</Treeview>
First, in the resources of the Treeview, rewrite systemcolors. highlightbrushkey as the system highlighted background color to transparent. Note that the system highlighted text color systemcolors. highlighttextbrushkey is changed to the default foreground color. In this case, run the program again. When the treeviewitem is selected, no color changes will occur.
The following describes how to define the headertemplate of a treeviewitem. The headertemplate is used to completely customize the appearance of a treeviewitem. Then, when the treeviewitem. isselected attribute is true, adjust the member attribute of the headertemplate. (Note that headertemplate defines the datatemplate object instead of controltemplate. The data is the header attribute of headereditemscontrol. Therefore, you need to use the templatedparent mode of relativesource in Data Binding to obtain the value of treeviewitem. isselected ).
All code:
<Treeview>
<Treeview. Resources>
<Solidcolorbrush X: Key = "{X: static systemcolors. highlightbrushkey}" color = "Transparent"/>
<Solidcolorbrush X: Key = "{X: static systemcolors. highlighttextbrushkey}" color = "black"/>
<Style targettype = "treeviewitem">
<Setter property = "headertemplate">
<Setter. value>
<Datatemplate>
<Border name = "border" borderthickness = "2" borderbrush = "Transparent" padding = "2" cornerradius = "5">
<Textblock text = "{binding}" name = "textblock"/>
</Border>
<Datatemplate. triggers>
<Datatrigger binding = "{binding isselected, relativesource = {relativesource templatedparent}" value = "true">
<Setter targetname = "border" property = "borderbrush" value = "Navy"/>
<Setter targetname = "textblock" property = "foreground" value = "red"/>
</Datatrigger>
</Datatemplate. triggers>
</Datatemplate>
</Setter. value>
</Setter>
</Style>
</Treeview. Resources>
<Treeviewitem header = "Visual Studio" isexpanded = "true">
<Treeviewitem header = "Visual Studio 2012"/>
<Treeviewitem header = "Visual Studio 2010"/>
</Treeviewitem>
<Treeviewitem header = "sharpdevelop"/>
</Treeview>
When you bind the data to the treeviewitem, you only need to directly convert the above ememplate, as shown below:
<Treeview. itemtemplate>
<! -- The todo attribute is the Child member attribute of the treeviewitem in the actual viewmodel -->
<Hierarchicaldatatemplate itemssource = "{binding todo}">
<! -- Contents of datatemplate in headertemplate -->
</Hierarchicaldatatemplate>
</Treeview. itemtemplate>