ItemsCollection和替代行着色

时间:2009-10-07 14:54:44

标签: wpf styles itemscontrol

我有一个itemscollection,我希望有替代行着色,我已经看过如何做到这一点,但找不到任何东西,我认为这应该很简单,但也许我错过了一些东西。

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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;
    }
}

答案 2 :(得分:0)

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

另一种方法是让你做得更多,包括替换模板,就是使用ItemsControl.AlternationIndex上的Trigger,如this blog post所示。