views:

27

answers:

1

How would I set the background color for for alternating rows (say: 1st, 3rd, 5th, 7th...) in a ListView using .net 2.0.

+2  A: 

RTM here.

public sealed class BackgroundConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, 
        CultureInfo culture)
    {
        ListViewItem item = (ListViewItem)value;
        ListView listView = 
            ItemsControl.ItemsControlFromItemContainer(item) as ListView;
        // Get the index of a ListViewItem
        int index = 
            listView.ItemContainerGenerator.IndexFromContainer(item);

        if (index % 2 == 0)
        {
            return Brushes.LightBlue;
        }
        else
        {
            return Brushes.Beige;
        }
    }
typoknig
Which name space (.net 2.0) contains IValueConverter interface?
Samir
You can code around that interface, you could populate the list with a for loop if you had to. The important part of this code for your purposes is the `if` statement with the mod operator.
typoknig

related questions