2

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

1 Answer 1

2

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;
        }
    }
2
  • Which name space (.net 2.0) contains IValueConverter interface?
    – Samir
    Aug 2, 2010 at 6:27
  • 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. Aug 2, 2010 at 6:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.