ListView 实现子项(ListViewItem)颜色间隔显示的样式。
1、定义一个ListViewItem样式
<Style TargetType="{x:Type ListViewItem}" x:Key="CustomerItemStyle">
<Setter Property="Background" Value="LightGreen"/>
<Style.Triggers>
<Trigger Property="ListBox.AlternationIndex" Value="1">
<Setter Property="Background" Value="#FF56BCAB"/>
</Trigger>
</Style.Triggers>
</Style>
2、样式引用和修改
<ListView x:Name="student_lv" AlternationCount="2" ItemContainerStyle="{StaticResource CustomerItemStyle}" Margin="10,0" Height="300">
<ListView.View>
<GridView>
<GridViewColumn Header="ID" DisplayMemberBinding="{Binding Path=StudentID}" Width="40"/>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=StudentName}" Width="80"/>
<GridViewColumn Header="Age" DisplayMemberBinding="{Binding Path=StudentAge}" Width="60"/>
</GridView>
</ListView.View>
</ListView>
3、后台对ItemsSource赋值,添加列表数据内容
public partial class ListViewStyle : UserControl
{
public ListViewStyle()
{
InitializeComponent();
for (int i = 0; i < 5; i++)
{
StudentsList.Add(new Student() { StudentAge = 12 + i, StudentID = i, StudentName = "JoanVan" + i });
}
this.student_lv.ItemsSource = StudentsList;
}
public List<Student> StudentsList = new List<Student>();
public class Student
{
public int StudentID { get; set; }
public string StudentName { get; set; }
public int StudentAge { get; set; }
}
}
4、效果图
**************************************************************************************************************