More on ListView styles

In my last post I described how to display a ListView control with alternating row colors. This gives the ListView the look of an accounting ledger. I ended by mentioning that there is a new feature in the .NET 3.5 SP1 libraries that makes this much easier. Microsoft added a new property to ItemsControl named AlternationCount. This technique works for any ItemsControl, not just the ListView demonstrated here.

Set AlternationCount to the number of rows that are to partake in the pattern. In this example a 2 row pattern is specified but any number may be used. You only must specify display properties for each row in the pattern. More on that later.

<Style x:Key="{x:Type ListView}" TargetType="{x:Type ListView}">
  <Setter Property="AlternationCount" Value="2"/>

The ItemsControl, a ListView in this example, attaches the property ItemsControl.AlternationIndex to each of its item containers. The value of ItemsControl.AlternationIndex is the row ordinal mod n, where n is the value specified for AlternationCount. In the example here it will be set to 0 for even numbered rows and 1 for the odd numbered ones. Now simply add property triggers on ItemsControl.AlternationIndex to the item container style. The item container in this example is a ListViewItem.

<Style x:Key="{x:Type ListViewItem}" TargetType="ListViewItem">
    <Trigger Property="ItemsControl.AlternationIndex" Value="0">
      <Setter Property="Background" Value="{StaticResource ListViewLine2Brush}"/>
    <Trigger Property="ItemsControl.AlternationIndex" Value="1">
      <Setter Property="Background" Value="{StaticResource ListViewLine1Brush}"/>

This technique does not suffer from the same problem detailed in my last post. The ItemsControl automatically updates AlternationIndex on all items affected by an adds or deletes on the underlying collection. No special workaround code needs to be written like in the last post. All source code for both of my ListView posts is located here.


3 Responses to “More on ListView styles”

  1. 1 blackmar June 6, 2009 at 5:33 pm

    How do I do 3 rows of one color and 3 rows of the next instead of one and one?

  2. 2 Aaron July 10, 2009 at 2:56 pm

    Is there a way to do this with an ItemsControl instead of a ListView? The problem I’m running into is that the ItemContainer for an ItemsControl is a ContentPresenter and not a ListViewItem as in the sample. As such there is no background property on the ContentPresenter. I suppose I would need to access the child somehow from the trigger setter. Any ideas?

  1. 1 More on XML Data Binding « Discovering .NET Trackback on January 12, 2009 at 11:33 am

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

January 2009
« Dec   Feb »
I am a part of all that I have met;
Yet all exprience is an arch whitherthro'
Gleams that untravell'd world, whose margin fades
For ever and for ever when I move.
How dull it is to pause, to make an end,
To rust unburnish'd, not to shine in use!
Alfred, Lord Tennyson

%d bloggers like this: