WPF ListBox等列表控件隔行换色
作者:互联网
<Style x:Key="NormalListBoxItemContainerStyle" TargetType="{x:Type ListBoxItem}"> <Setter Property="SnapsToDevicePixels" Value="True"/> <Setter Property="HorizontalContentAlignment" Value="Stretch"/> <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ItemsControl}}}"/> <Setter Property="Background" Value="Transparent" /> <Setter Property="BorderBrush" Value="Transparent"/> <Setter Property="BorderThickness" Value="0"/> <Setter Property="IsTabStop" Value="False"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListBoxItem}"> <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Border> <ControlTemplate.Triggers> <Trigger Property="ItemsControl.AlternationIndex" Value="0"> <Setter TargetName="Bd" Property="Background" Value="Red"/> </Trigger> <Trigger Property="ItemsControl.AlternationIndex" Value="1"> <Setter TargetName="Bd" Property="Background" Value="Green"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
同时,ListBox需要设置AlternationCount属性,AlternationCount=2代表2行一个循环,3代表3行一个循环
标签:控件,换色,隔行,循环,AlternationCount,ListBox 来源: https://www.cnblogs.com/lopengye/p/16194508.html