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