I am using a datagrid and changing the color of the rows according to their conditions and I am performing this programmatically. follow the example as my datagrid is bound to a datatable I load information straight from the datatable
private void UpdateCor () {
gvDados.UpdateLayout ();
for (int i = 0; i <dt.Rows.Count; i ++)
{
var rowContext = (DataGridRow)
gvDados.ItemContainerGenerator.ContainerFromIndex (i);
if (rowContext! = null)
{
if (dt.Rows [i] ["situation"]. ToString (). Equals (1))
rowContext.Background = Brushes.Green;
else
rowContext.Background = Brushes.Red;
}
}
}
With this I can update the color of my grid even though it is not the best method to be approached. my problem is this, whenever I use the scroll to go down or up the bar the colors become outdated. How do I prevent this from happening? that even when I roll the bar the colors stay fixed?
This is a similar question to this question. Can be done using datatrigger:
<DataGrid ItemsSource="{Binding YourItemsSource}">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding State}" Value="State1">
<Setter Property="Background" Value="Red"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding State}" Value="State2">
<Setter Property="Background" Value="Green"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</DataGrid>