以编程方式更改 ListView 行的背景颜色 (wpf) [英] Change background color of ListView row programmatically (wpf)

查看:85
本文介绍了以编程方式更改 ListView 行的背景颜色 (wpf)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,它通过传递对象列表来填充 ListView.该类使用反射来查看每个对象的属性以生成 ListView.如何更改 ListView 中一行的背景颜色.

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 could I change the background color of a row in the ListView.

这个页面完全符合我的要求寻找.唯一的问题是我的 ListView 绑定到对象列表.换句话说,ListView 的每一项都是一个被绑定的对象,而不是一个 ListViewItem.我假设这就是我无法将 ListView 中的某些项目转换为 ListViewItem 的原因.例如,当我这样做时:

This page does exactly what I am looking for. The only problem is that my ListView is bound to the list of objects. In other words each item of the ListView is an object that is bound instead of a ListViewItem. I am assuming that is the reason why I cannot cast some item in the ListView to a ListViewItem. For example when I do this:

ListViewItem someItem = (ListViewItem)listView1.Items[0];

我收到一个 InvalidcastException 因为如果我将对象物理添加到 ListView 的位置,例如:

I get an InvalidcastException because if I where to physically add the objects to the ListView like:

listview.items.add(someObject) 那么这将起作用,但是因为我将列表绑定到 ListView,该行不起作用.我想这就是我不能投的原因.我想投射它的原因是因为 ListViewItem 有一个 Background 属性.

listview.items.add(someObject) then this will work, but because I am binding the list to the ListView that line does not work. I think that is the reason why I am not able to cast. The reason why I want to cast it is becasue a ListViewItem has a Background property.

编辑

我可以使用我尝试过的前 12 个对象来做到这一点:

I am able to do that with the first 12 objects I have tried the folowing:

for (int i = 0; i < listView1.Items.Count; i++)
{
    var lvitem = listView1.ItemContainerGenerator.ContainerFromIndex(i) as ListViewItem;
    lvitem.Foreground = Brushes.Green;                
}

我收到此错误:

我也试过这个:

foreach (Tiro t in listView1.Items)
{
    var lvitem = listView1.ItemContainerGenerator.ContainerFromItem(t) as ListViewItem;
    if (t.numero == 0 || t.numero == 37)
    {
        //lvitem.Background = Brushes.Green;
        lvitem.Foreground = Brushes.Green;
    }
    else if (t.numero % 2 == 0)
    {
        //lvitem.Background = Brushes.Red;
        lvitem.Foreground = Brushes.Red;
    }
    else
    {
        //lvitem.Background = Brushes.Gray;
        lvitem.Foreground = Brushes.Black;
    }

}

我得到同样的错误:

我不明白为什么lvitem在12次迭代后为null?

I don't understand why lvitem is null after the 12 iteration?

它只适用于正在显示的项目....

It only works with the items that are being displayed....

推荐答案

当使用 ItemContainerGenerator 时,请注意容器是异步生成的.生成器公开了一个您可以收听的状态更改事件:

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.

这篇关于以编程方式更改 ListView 行的背景颜色 (wpf)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆