AlternationConverter的使用
xaml
<Window.Resources>
<AlternationConverter x:Key="AlternationConverter">
<SolidColorBrush Color="Red" />
<SolidColorBrush Color="Orange" />
<SolidColorBrush Color="Yellow" />
<SolidColorBrush Color="Green" />
<SolidColorBrush Color="Cyan" />
<SolidColorBrush Color="Blue" />
<SolidColorBrush Color="Purple" />
</AlternationConverter>
</Window.Resources>
<Grid>
<ListBox x:Name="box" AlternationCount="7">
<ListBox.ItemTemplate>
<DataTemplate>
<Border
Width="10"
Height="10"
Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=(ItemsControl.AlternationIndex), Converter={StaticResource AlternationConverter}}" />
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Grid>
xaml.cs
box.ItemsSource = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
效果

浙公网安备 33010602011771号