How to paint a cell from a Datagrid?

Asked

Viewed 335 times

1

See the code in How to change the color of a Datagrid line in C#?

He’s painting the whole line, as far as that’s concerned, what happens and that at the time I give one scroll the painted line is deregulated.

Example: I painted line #1 when scrolling Datagrid with scroll the line does not keep painted. rolling the color for other lines.

  • Is there any relationship between the color of the cell and the value it contains?

  • There is no relation between the color of the cell and the value contained in it.

1 answer

2


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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.