Reputation: 59979
I have a datagrid, that has as its base an XML file. Depending on the data in the xml, the list gets sorted.
What i now want to do is change the first 3 lines background color, for example the first one red, second yellow and 3rd blue. after the 3rd it can all be just default white.
I cant find how to do this, anyone could help me out ?
thanx!
Upvotes: 0
Views: 693
Reputation: 30127
Most simplest way would be to handle the LoadingRow
event of DataGrid
and update the colors inside it.
private void dg_LoadingRow(object sender, System.Windows.Controls.DataGridRowEventArgs e)
{
int index = e.Row.GetIndex();
if (index == 0)
e.Row.Background = Brushes.Blue;
else if (index == 1)
e.Row.Background = Brushes.Red;
else if (index == 2)
e.Row.Background = Brushes.White;
}
another way could be to retrieve the first three DataGridRow
from DataGrid
using the method described in this post. Then use the dataGridRow's Background
property to change it's color
A more cleaner way would be to define a style
for DataGridRow
and use triggers
to change the background
color of particular DataGridRow
. I am not sure if you have some criteria behind changing background
color of first three rows or it is a hard coded requirement. If it is some criteria or value in DataGrid
then you should go for Style
based approach.
Upvotes: 1