ItemsCollection和交替行着色

3

我有一个items集合,想实现交替行颜色,我看过一些教程但没有找到合适的方法,我觉得这应该是很简单的,但可能是我漏掉了什么。

顺便说一下,这是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>
3个回答

7

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 版本。


当我使用你的示例时,遇到了无法解析样式属性“background”的问题。 - RubbleFord
代码现在可以编译了,但是很遗憾没有交替颜色,我会继续查找。 - RubbleFord

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

0

你正在使用3.5 SP1吗?如果你只想改变一些属性(比如背景),那么最简单的方法可能是使用转换器,就像MSDN上的这个例子

另一种选择,可以让你做更多的事情,包括替换模板,就是在ItemsControl.AlternationIndex上使用触发器,就像这篇博客文章中所演示的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接