在这个例子中,我们使用了两个DataTrigger,它们分别检查ContentPresenter的AlternationIndex属性是否为0或1。如果AlternationIndex为0,TextBlock的背景颜色将设置为#07FAFE;如果AlternationIndex为1,TextBlock的背景颜色将设置为#022A3C。
<ItemsControl AlternationCount="2" ItemsSource="{Binding ListApple}" Margin="0" Padding="0">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" Width="29" FontSize="10" Margin="0 0 1 0" Height="20" Padding="0 3 0 0" TextAlignment="Center">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContentPresenter}, Path=(ItemsControl.AlternationIndex)}" Value="0">
<Setter Property="Background" Value="#07FAFE"/>
<Setter Property="Foreground" Value="#022A3C" />
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContentPresenter}, Path=(ItemsControl.AlternationIndex)}" Value="1">
<Setter Property="Background" Value="#022A3C"/>
<Setter Property="Foreground" Value="#FFFFFF" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
这样,ItemsControl中的TextBlock就会按照奇数和偶数索引来交替使用不同的背景颜色。你可以根据需要调整颜色和AlternationCount的值。