Metro app - ListView - how to alternate background colour of ListViewItems

前端 未结 4 824
没有蜡笔的小新
没有蜡笔的小新 2020-12-19 04:26

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

相关标签:
4条回答
  • 2020-12-19 04:53

    I believe the code sample here is useful https://msdn.microsoft.com/en-us/library/ms750769(v=vs.85).aspx

    0 讨论(0)
  • 2020-12-19 04:53

    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.

    0 讨论(0)
  • 2020-12-19 04:56

    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

    0 讨论(0)
  • 2020-12-19 04:59

    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.

    0 讨论(0)
提交回复
热议问题