I've got an itemscollection and I want to have alternate row colouring, I've looked about how to do this but can't find anything, I think this should be simple but maybe I'm missing something.
It's WPF btw.
<Grid>
<ItemsControl Name="itemsControl">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="80"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Path=name}" VerticalAlignment="Center"/>
<TextBlock Grid.Column="1" Text="{Binding Path=something}" VerticalAlignment="Center"/>
<Button Grid.Column="2" Content="Launch" Tag="{Binding}" Height="25" VerticalAlignment="Center" Click="Button_Click"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Control.Margin" Value="5"/>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
<Button Height="23" HorizontalAlignment="Right" Margin="0,0,12,12" Name="button1" VerticalAlignment="B开发者_运维问答ottom" Width="75" Click="button1_Click">Button</Button>
</Grid>
Add AlternationCount="2"
to your ItemsControl.
Then add this to your ItemContainerStyle to get alternating red/blue items:
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Control.Background" Value="Red"></Setter>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Control.Background" Value="Blue"></Setter>
</Trigger>
</Style.Triggers>
Edit: you need to have the .NET 3.0/3.5 SP1.
You need 2 styles (one for regular row and the other for alternate row of course) and a style selector for alternate rows. This worked for me:
XAML:
<Window.Resources>
<Style x:Key="theAlternateStyle">
<Setter Property="ListBoxItem.Background" Value="LightGray" />
</Style>
<Style x:Key="theDefaultStyle">
<Setter Property="ListBoxItem.Background" Value="Blue" />
</Style>
</Window.Resources>
<Grid Margin="4">
<ListBox DisplayMemberPath="Name" ItemsSource="{Binding}">
<ListBox.ItemContainerStyleSelector>
<local:AlternatingRowStyleSelector AlternateStyle="{StaticResource theAlternateStyle}" DefaultStyle="{StaticResource theDefaultStyle}" />
</ListBox.ItemContainerStyleSelector>
</ListBox>
</Grid>
Row Selector (C#):
public class AlternatingRowStyleSelector : StyleSelector
{
public Style DefaultStyle { get; set; }
public Style AlternateStyle { get; set; }
// Flag to track the alternate rows
private bool isAlternate = false;
public override Style SelectStyle(object item, DependencyObject container)
{
// Select the style, based on the value of isAlternate
Style style = isAlternate ? AlternateStyle : DefaultStyle;
// Invert the flag
isAlternate = !isAlternate;
return style;
}
}
Are you using 3.5 SP1? If it's a few properties you want to change (like background) then the easiest method is probably a converter as in this example on MSDN.
An alternative, and one that let you do a fair bit more, including replacing the template, would be to use a Trigger on the ItemsControl.AlternationIndex as demonstrated in this blog post.
精彩评论