The AlternationCount and ItemsControl.AlternationIndex properties of Itemcontrol class enable the user to specify the appearance for two or more alternating item containers.
The AlternationCount is the number of rows that form a sequence. If AlternationCount is set to 1, the list will alternate after every item.
Each Item(here DataGridItem) have an ItemsControl.AlternationIndex which allow the user to determine how it is numbered in the sequence. ItemsControl.AlternationIndex begins at 0, increments until it is (AlternationCount - 1), and then restarts at 0. So if the AlternationCount is 2 we can apply style to 2 Items(First Items's AlternationIndex =0, Second Item's AlternationIndex =1 and Third Item's AlternationIndex = 0, ie it will reset.)
<Window.Resources>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="AntiqueWhite"/>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="PaleGoldenrod"/>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="2">
<Setter Property="Background" Value="Lavender"/>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="3">
<Setter Property="Background" Value="Pink"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<DataGrid x:Name="GridViewNames" AlternationCount="4">
<DataGrid.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=FistName}"/>
<TextBlock Text="{Binding Path=LastName}"/>
</StackPanel>
</DataTemplate>
</DataGrid.ItemTemplate>
</DataGrid>
</Grid>
Refrence:http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.alternationcount.aspx
0 comments:
Post a Comment