ListView控件编程行的更改背景颜色(WPF) [英] Change background color of ListView row programmatically (wpf)

查看:837
本文介绍了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该行不起作用。我想这就是为什么我不能够施展的原因。我为什么要投它是一个becasue有ListViewItem的背景一个属性的原因。

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迭代后为空?

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.

另一种选择,而不是在code栋ListView的项目是使用数据模板。你可能有几个属性添加到了,虽然显示目的您的视图模式。

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天全站免登陆