ItemsCollection and alternative string coloring

I have an itemcollection and I want to have an alternative string coloring, I was looking for how to do this, but I can’t find anything, I think it should be simple, but maybe something is missing for me.

This is 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
source share
3 answers

Add AlternationCount="2"an ItemsControl to your element.

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 .NET 3.0 / 3.5 SP1.

+6
source

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>

(#):

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

Are you using 3.5 SP1? If there are several properties that you want to change (for example, background), then the simplest method is probably the converter, as in this example in MSDN .

An alternative and one that will allow you to make a fairer bit, including replacing the template, will use a trigger for ItemsControl.AlternationIndex, as shown in this blog post .

0
source

Source: https://habr.com/ru/post/1719523/


All Articles