Change background color of ListView row programmatically (wpf)

前端 未结 6 1815
再見小時候
再見小時候 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:10

    When using the ItemContainerGenerator then be aware that the containers are generated asynchronously. The generator exposes a status changed event you could listen to:

    listView.ItemContainerGenerator.StatusChanged += new EventHandler(ContainerStatusChanged);     
    
    private void ContainerStatusChanged(object sender, EventArgs e)  
    {  
        if (listView.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)  
        {  
            foreach (Tiro t in listView1.Items)
            {
                ...
            }
        }  
    }
    

    Not sure if that will create any weird drawing effects (flickering) or not.

    Another option instead of building the listview items in code is to you use data templates. You might have to add a few properties to your view model for display purposes though.

提交回复
热议问题