Reputation: 313
I have a ListView that regularly update and show some analyzed data.
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.
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. 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.
Upvotes: 0
Views: 1863
Reputation: 313
I solved the problem pretty easily after I search for the CellTemplate usage, Thanks to Sean Manton's reply.
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".
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:
<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.
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.
Upvotes: 2
Reputation: 146
GridViewColumn
has a CellTemplate
property that you can point to a DataTemplate
for the cells. In the DataTemplate
you can use a Binding Converter to map the values to the desired styling.
Upvotes: 2