ListBox控件要实现斑马线的效果,则需要修改ListBoxItem的样式和设置ListBox中的AlternationCount属性,即可实现斑马线的效果,下面我们来看一下效果图:
这里我设置Item颜色交替变化为3,每3行一个轮回。
AlternationCount:获取或设置 ItemsControl 中的交替项容器的数目,该控件可使交替容器具有唯一外观。使用 AlternationCount 和 ItemsControl.AlternationIndex 属性可以指定两个或多个交替项容器的外观。
例如,如果 AlternationCount 为 3,并且有七个项目, ItemsControl下表列出了 ItemsControl.AlternationIndex 每个项。看下面的表格,就很容易理解了。
1 | 0 |
2 | 1 |
3 | 2 |
4 | 0 |
5 | 1 |
6 | 2 |
7 | 0 |
实现代码.xaml
<UserControl.Resources>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Background" Value="#18a2d0"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="MinHeight" Value="30"/>
<Setter Property="Margin" Value="1"/>
<Style.Triggers>
<Trigger Property="ListBox.AlternationIndex" Value="1">
<Setter Property="Background" Value="#008000"/>
</Trigger>
<Trigger Property="ListBox.AlternationIndex" Value="2">
<Setter Property="Background" Value="#0952dc"/>
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type ListBox}">
<Setter Property="AlternationCount" Value="3"/>
</Style>
</UserControl.Resources>
**************************************************************************************************************