Issues encountered during WPF development and solutions series (3): how to change the background color of each row in listview (the background color produces an alternate effect)

Source: Internet
Author: User

Method 1: Define a style that uses ivalueconverter to make the background color generate an alternate Effect
The following example shows how to define a style for the listviewitem control that binds the value of the background property to ivalueconverter.

<Style X: Key = "myitemstyle" targettype = "{X: Type listviewitem}">
<Setter property = "background">
<Setter. value>
<Binding relativesource = "{relativesource self }"
Converter = "{staticresource myconverter}"/>
</Setter. value>
</Setter>
</Style>


The following example defines the resourcekey for ivalueconverter. The following example defines the resourcekey for ivalueconverter. <Namespc: backgroundconverter X: Key = "myconverter"/>


The following example shows the definition of ivalueconverter that sets the background Attribute Based on the row index. The following example shows the definition of ivalueconverter that sets the background Attribute Based on the row index. Public sealed class backgroundconverter: ivalueconverter
{
Public object convert (object value, type targettype, object parameter,
Cultureinfo Culture)
{
Listviewitem item = (listviewitem) value;
Listview =
Itemscontrol. itemscontrolfromitemcontainer (item) as listview;
// Get the index of a listviewitem
Int Index =
Listview. itemcontainergenerator. indexfromcontainer (item );

If (index % 2 = 0)
{
Return brushes. lightblue;
}
Else
{
Return brushes. Beige;
}
}

The following example shows how to define a style as its itemcontainerstyle to provide the listview for the desired layout. The following example shows how to define a style as its itemcontainerstyle to provide the listview for the desired layout. <Listview name = "thelistview"
Itemssource = "{binding source = {staticresource employeedata },
XPath = employee }"
Itemcontainerstyle = "{staticresource myitemstyle}">
<Listview. View>
<Gridview>
<Gridviewcolumn displaymemberbinding = "{binding XPath = firstname }"
Header = "first name" width = "120"/>
<Gridviewcolumn displaymemberbinding = "{binding XPath = lastname }"
Header = "last name" width = "120"/>
<Gridviewcolumn displaymemberbinding = "{binding XPath = favoritecity }"
Header = "favorite city" width = "120"/>
</Gridview>
</Listview. View>
</Listview>


For the other 2 methods, see msdn: http://msdn2.microsoft.com/zh-cn/library/ms750769.aspx

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.