简体   繁体   English

如何在 WPF 中分别更改 ListView 列(每行的每个单元格)的颜色?

[英]How to change the color of a ListView column (each cell of each row) seperately in WPF?

I have a ListView that regularly update and show some analyzed data.我有一个定期更新和显示一些分析数据的 ListView。
Each row represent a different source and the columns show it's analyzed values.每行代表一个不同的来源,而列显示它的分析值。

What I want is to only change the Background color or Font Color of a single cell if a certain criteria is met.我想要的是仅在满足特定条件时更改单个单元格的背景颜色或字体颜色。

For example if the "Price" Column value become less than X, it will change into Red Color background or font.例如,如果“价格”列值变得小于 X,它将变为红色背景或字体。
But I don't want the whole row to be changing color, but only the "Price" Column of that certain row.但我不希望整行都在改变颜色,而只是该特定行的“价格”列。

I know how to change the color of a whole row, there has been many example of that in here as well, for example using binding.我知道如何更改整行的颜色,这里也有很多这样的例子,例如使用绑定。 But I want to know if there is anyway to just change the Background Color or Font Color of a single Cell.但我想知道是否有办法改变单个单元格的背景颜色或字体颜色。

Thanks.谢谢。

Edit:编辑:
Thanks to Sean Manton's Reply, I solved this problem pretty easily.感谢 Sean Manton 的回复,我很容易地解决了这个问题。 It's surprising that I had so much problem finding any result in my searches for this, so I added my own answer with a working example as well.令人惊讶的是,我在搜索此内容时找不到任何结果,所以我也用一个工作示例添加了我自己的答案。

GridViewColumn has a CellTemplate property that you can point to a DataTemplate for the cells. GridViewColumn有一个CellTemplate属性,您可以指向单元格的DataTemplate In the DataTemplate you can use a Binding Converter to map the values to the desired styling.DataTemplate中,您可以使用绑定转换器将 map 值转换为所需的样式。

I solved the problem pretty easily after I search for the CellTemplate usage, Thanks to Sean Manton's reply.搜索CellTemplate的用法后,我很容易解决了这个问题,感谢Sean Manton的回复。

Not sure why it was so hard for me to find this when I searched for changing color of ListView Column or Cell and the result only showed it for "Row" and not "Column".不知道为什么当我搜索 ListView 列或单元格的更改颜色时我很难找到它,结果只显示“行”而不是“列”。

Also, Most of the answers here always use a Binding Converter, which seems to be the better way to do it, But I noticed this can work even without any converter and with a very simple code as well.此外,这里的大多数答案总是使用绑定转换器,这似乎是更好的方法,但我注意到即使没有任何转换器并且使用非常简单的代码也可以工作。

So I will write this example here in case anyone find it useful:所以我会在这里写这个例子,以防有人觉得它有用:

XAML Design: XAML 设计:

<ListView x:name="myListView">
    <ListView.View>
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding ID}" Header="ID" />
            <GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name" />
            <GridViewColumn Header="Price">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Price}" Foreground="{Binding Price_Color}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

As for the C# Code, We just have to make a list which include the "Price_Color" string value as well.至于 C# 代码,我们只需要制作一个包含“Price_Color”字符串值的列表即可。

public class myListViewItem
{
    public int ID { get; set; }
    public string Name { get; set; }
    public double Price { get; set; }
    public string Price_Color { get; set; }
}

And now we can very easily add and customize the items with our desired color like this:现在我们可以很容易地添加和自定义具有我们想要的颜色的项目,如下所示:

List<myListViewItem> myItemList = new List<myListViewItem>();
myItemList.Add(new myListViewItem { ID = 1, Name = "Book", Price = 15.7, Price_Color = "Green" });
myItemList.Add(new myListViewItem { ID = 2, Name = "Laptop", Price = 4000, Price_Color = "Red" });
myItemList.Add(new myListViewItem { ID = 2, Name = "Mobile", Price = 3000, Price_Color = "#FFF2CC" });
MyListView.ItemsSource = myItemList;

Now we will have 3 row in our ListView each one having a different font color for the "Price" Column only.现在我们的 ListView 中将有 3 行,每一行仅对“价格”列具有不同的字体颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM