Change background color of ListView row programmatically (wpf)

前端 未结 6 1812
再見小時候
再見小時候 2020-12-09 20:53

I have a class that populates a ListView by passing a list of objects. The class uses reflection to see the properties of each object in order to generate the ListView. How

6条回答
  •  粉色の甜心
    2020-12-09 21:27

    You could use the ItemContainerGenerator, e.g:

    var lvitem = listView.ItemContainerGenerator.ContainerFromItem(item) as ListViewItem;
    var lvitem = listView.ItemContainerGenerator.ContainerFromIndex(0) as ListViewItem;
    

    However by default the ListView is virtualizing, this means ListViewItems are created on the fly as needed (only if the item is actually visible in the list), so the above methods will not return containers for items which are currently not visible.

    This being the case it usually is preferable to define a binding on the Background property via a Setter in the ItemContainerStyle.

提交回复
热议问题