2009-10-07 27 views
3

我有一个itemscollection,我想要备用行着色,我已经看过如何做到这一点,但找不到任何东西,我认为这应该很简单,但也许我是遗漏了什么。ItemsCollection和备用行着色

这是WPF顺便说一句。

<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="Bottom" Width="75" Click="button1_Click">Button</Button> 
    </Grid> 

回答

6

AlternationCount="2"添加到您的ItemsControl中。

然后添加到您的ItemContainerStyle获得交替的红色/蓝色物品:

<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> 

编辑:你需要有.NET 3.0/3.5 SP1

+0

我得到使用你的榜样 – RubbleFord

+0

尝试Control.Background –

+0

代码编译时无法解析样式属性背景现在,但没有交替的颜色不幸,我会继续挖。 – RubbleFord

0

您使用3.5 SP1吗?如果你想改变一些属性(如背景),那么最简单的方法可能是一个转换器,如this example on MSDN

另一种方法是让你做更合理的一点,包括替换模板,就是在ItemsControl.AlternationIndex上使用触发器,如this blog post所示。

2

您需要2个样式(一个用于常规行,另一个用于交替行当然)和一个用于交替行的样式选择器。这为我工作:

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> 

行选择器(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; 
    } 
}