In my Metro style app for Windows 8, I\'m binding a Listview to an ObservableCollection and I would like the background color of each ListViewItem to alternate (white, gray
I believe the code sample here is useful https://msdn.microsoft.com/en-us/library/ms750769(v=vs.85).aspx
Never tried to do styling like this but what if:
you bind a background color to some property and that property will be set via IValueConverter according to maybe index of current item in listview.
If I make any sense.
Edit:
Funny thing, while i was writing my answer, Filip Skakun came with exactly same idea.
I search online and found a technique that included adding an index property to the model in question and then adding a converter to the DataTemplate. This wasn’t ideal because it only changed the contents of the list item, so depending on padding and content alignment you’d see gaps around the row background. I also didn’t like the code smell of modifying my data model objects with UI code.
Try this it will help, http://www.bendewey.com/index.php/523/alternating-row-color-in-windows-store-listview
You can use a converter - grab a row index from an item and convert it to a brush. Also - if ItemTemplate does not give you enough control - use ItemContainerStyle to modify the brush at the ListViewItem template level.
Another option might be to specify an ItemTemplateSelector that gives you a different template with a different brush depending on an item. You would still need to generate row indices though or somehow enable the selector to determine if the item is at an even or odd position.