Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use AlternationIndex in ItemsControls?

I have seen some articles that show how to use AlternationIndex with ListBoxes or ListViews, but I had spent a few hours trying to get alternating background colors on the base ItemsControl class and nothing seems to work. All ListBox samples I saw use ListBoxItem as the target type for the style that sets the background based onAlternationIndex - like this one from MSDN:

<Grid>     <Grid.Resources>         <Style x:Key="alternatingWithTriggers" TargetType="{x:Type ListBoxItem}">             <Setter Property="Background" Value="Blue"/>             <Setter Property="Foreground" Value="White"/>             <Style.Triggers>                 <Trigger Property="ListBox.AlternationIndex" Value="1">                     <Setter Property="Background" Value="CornflowerBlue"/>                     <Setter Property="Foreground" Value="Black"/>                 </Trigger>                 <Trigger Property="ListBox.AlternationIndex" Value="2">                     <Setter Property="Background" Value="LightBlue"/>                     <Setter Property="Foreground" Value="Navy"/>                 </Trigger>             </Style.Triggers>         </Style>      </Grid.Resources>     <ListBox AlternationCount="3" ItemsSource="{StaticResource data}"               ItemContainerStyle="{StaticResource alternatingWithTriggers}">     </ListBox> </Grid> 

I want to use the ItemsControl because I do not want the selection functionality and I think restyling a ListBox to hide it might not be the best choice.

This is one of the things I was trying:

<DataTemplate DataType="{x:Type vm:ObservableCollectionItem}">     <Grid>         <!-- some content here -->     </Grid> </DataTemplate>  <!-- ... -->  <ItemsControl     ItemsSource="{Binding ObservableCollectionItems}"     AlternationCount="2" >     <ItemsControl.ItemContainerStyle>         <Style>             <Style.Triggers>                 <Trigger Property="ItemsControl.AlternationIndex" Value="0">                     <Setter Property="Grid.Background" Value="Red"></Setter>                 </Trigger>                 <Trigger Property="ItemsControl.AlternationIndex" Value="1">                     <Setter Property="Grid.Background" Value="Blue"></Setter>                 </Trigger>             </Style.Triggers>         </Style>     </ItemsControl.ItemContainerStyle> </ItemsControl> 

The problem I saw was that the visual tree has a list of ContentPresenters that have ItemsControl.AlternationIndex alternate between 0 and 1, but the Grid in each ContentPresenter has ItemsControl.AlternationIndex set to 0.

There is probably something obvious I am missing...

like image 382
Filip Skakun Avatar asked Aug 25 '10 15:08

Filip Skakun


2 Answers

The ItemContainerStyle is applied to the elements generated by the ItemsControl: ContentPresenter. The ContentPresenter will in turn contain whatever you put in your ItemTemplate. In the case of a ListBox, the ItemContainerStyle is applied to the generated ListBoxItem.

The AlternationCount is, based on what you posted, only available on these generated items. You cannot use the ItemContainerStyle to set the Grid's background, because the Grid is unknown to that Style.

The following would be ideal, but unfortunately ContentPresenter has no background property. It would work for a ListBox (with ListBoxItems) however.

<ItemsControl     ItemsSource="{Binding ObservableCollectionItems}"     AlternationCount="2">     <ItemsControl.ItemContainerStyle>         <Style TargetType="ContentPresenter">             <Style.Triggers>                 <Trigger Property="ItemsControl.AlternationIndex" Value="0">                     <Setter Property="Background" Value="Red"></Setter>                 </Trigger>                 <Trigger Property="ItemsControl.AlternationIndex" Value="1">                     <Setter Property="Background" Value="Blue"></Setter>                 </Trigger>             </Style.Triggers>         </Style>     </ItemsControl.ItemContainerStyle> </ItemsControl> 

So you end up writing a style for the grid which binds to the AlternationIndex of your parent ContentPresenter.

<DataTemplate DataType="{x:Type vm:ObservableCollectionItem}">     <Grid>         <Grid.Style>             <Style TargetType="Grid">                 <Style.Triggers>                     <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}}, Path=(ItemsControl.AlternationIndex)}" Value="0">                         <Setter Property="Background" Value="Red"/>                     </DataTrigger>                     <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}}, Path=(ItemsControl.AlternationIndex)}" Value="1">                         <Setter Property="Background" Value="Blue"/>                     </DataTrigger>                 </Style.Triggers>             </Style>         </Grid.Style>     </Grid> </DataTemplate> 
like image 200
Bubblewrap Avatar answered Sep 19 '22 19:09

Bubblewrap


hm... After about 2 hours of playing around, I finally found the solution that simply works:

<ItemsControl ItemsSource="{Binding}" AlternationCount="2">     <ItemsControl.ItemTemplate>         <DataTemplate>             <Grid Background="Transparent" x:Name="__PART_GRID"></Grid>             <DataTemplate.Triggers>                 <Trigger Property="ItemsControl.AlternationIndex" Value="0">                     <Setter TargetName="__PART_GRID" Property="Background" Value="Red"/>                 </Trigger>                 <Trigger Property="ItemsControl.AlternationIndex" Value="1">                     <Setter TargetName="__PART_GRID" Property="Background" Value="Blue"/>                 </Trigger>             </DataTemplate.Triggers>         </DataTemplate>     </ItemsControl.ItemTemplate> </ItemsControl> 

I hope this answer helps others to save some time.

like image 29
Nachbars Lumpi Avatar answered Sep 21 '22 19:09

Nachbars Lumpi