1

I have a Windows 8.1 application.

My CollectionViewSource is a list of items which is grouped by date when the items were created. Now I have binded this CollectionViewSource to a ListView so as to display the group headers for each group and then the corresponding values.

Let's say I have 3 groups as follows

September 1
    Item-1
    Item-2
    Item-3
September 2
    Item-4
    Item-5
September 3
    Item 6

Now I want to display alternate items in each group with alternate backgrounds. If Item-1 is black, then Item-2 is white, Item-3 is black. Since Item-4 is in group 2 it is again black and so on. If I get the index of each element in each group, I can do this alternate backgrounds using a converter. How do I get the index?

Here is my xaml of my ListViewItemTemplate

<DataTemplate x:Key="MyListViewItemTemplate">
    <Grid Background="{Binding Converter={StaticResource alternateListItemBackgroundConverter}}">
    </Grid>
</DataTemplate>

What should I bind in the above xaml to get the index which I can use in my converter as shown below. Here is my Convert function of the converter

public object Convert(object value, Type targetType, object parameter, string language)
{
    int index = value as int;
    if (value == null || !int.TryParse(value.ToString(), out index))
    {
        throw new ArgumentException("The value passed to this converter must be an integer value", "value");
    }
    return index % 2 == 0 ? Colors.Black : Colors.White;
}

I would be very glad if someone can point me in the right direction. Thanks in Advance.

HelpMatters
  • 1,299
  • 1
  • 13
  • 32

1 Answers1

0

The typical solution might be to set the index property value in the grouping function or whatever it is you use to group the items or generate the collection view.

Filip Skakun
  • 31,624
  • 6
  • 74
  • 100