The problem you refer to while doing scroll has to do with how the visual elements of the Datagrid are managed.
The objects Datagridrow, Datagridcell etc, are created and discarded depending on whether visible or not.
Note that the code of the answer to that question requires the line to be visible in order to obtain a reference to the Datagridrow.
To prevent the problem from arising, cell color information has to be available whenever the cell needs to be recreated.
To change the background color of a cell, statically, set a Datagrid.Cellstyle
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Background" Value="Red" />
</Style>
</DataGrid.CellStyle>
All cells will have a red background.
If you want only one or a few cells to have their color changed, you have to store that information somewhere.
Usually the color is a function of the cell value.
Another way is the value of the cell, in this case a class, to have a property that indicates the color.
Example where the color is set in the property CorDoFundo
:
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="{Binding CorDoFundo}" />
</Style>
Is there any relationship between the color of the cell and the value it contains?
– ramaral
There is no relation between the color of the cell and the value contained in it.
– MeuChapeu