WPF: Maybe a shortcut to define the treeviewitem Template

Source: Internet
Author: User
Tags visual studio 2010 sharpdevelop

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.