How to paint a cell in a DataGrid?

1

See the code in How to change the color of a DataGrid row in C #?

He is painting the entire line, as far as that is concerned, what happens and that when I give a scroll the painted line deregulates.

Example: I painted line # 1 when scrolling the DataGrid with scroll the line does not remain painted. rolling the color to other lines.

    
asked by anonymous 03.11.2014 / 15:56

1 answer

2

The problem that you refer to when doing scroll has to do with how the visual elements of the DataGrid are managed. The DataGridRow, DataGridCell objects, etc., are created and dropped depending on whether they are visible or not. Note that the response code to this question requires the row to be visible in order to get a reference to the DataGridRow .

So that the problem does not arise, cell color information must be available whenever the cell needs to be re-created.

To change the background color of a cell statically, define 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 need to save this information somewhere. Normally the color is a function of the value of the cell. 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 CorDoFundo property:

<Style TargetType="DataGridCell">
    <Setter Property="Background" Value="{Binding CorDoFundo}" />
</Style>
    
03.11.2014 / 17:50