3

我有一个 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>
4

3 回答 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

于 2009-10-07T15:03:19.657 回答
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;
    }
}
于 2009-10-07T15:04:53.247 回答
0

您使用的是 3.5 SP1 吗?如果您想要更改一些属性(如背景),那么最简单的方法可能是转换器,如 MSDN 上的此示例中所示

另一种方法是让您做更多的事情,包括替换模板,是在 ItemsControl.AlternationIndex 上使用触发器,如本博客文章中所示。

于 2009-10-07T15:03:57.803 回答