How to control the entire background of a DataGridCell, and not just a part of the text?

I am trying to control the background of a DataGrid cell in a column subject to its value. Unfortunately, I get something like this:

alt text

Which is not very aesthetically pleasing, I would like the whole cell to be in a different color, and not just part of the text. Here is the piece of code:

<DataGridTextColumn
    Binding="{Binding Path=PrivateMemorySize, StringFormat='#,##0'}" 
    Header="Memory Size" >
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="HorizontalAlignment" Value="Right" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=PrivateMemorySize,
                            Converter={StaticResource isLarge}, 
                            ConverterParameter=20000000}" Value="true">
                    <Setter Property="Background" Value="Yellow" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

( isLarge- this is just a converter that returns truewhen the cell value is greater than the parameter)

If I define a style for the DataGridCell target, the result will be the same.

Any ideas what could be wrong? I do not use anything unusual, just the default DataGrid (which in this example is associated with CLR objects to populate the table).

+3
source share
1

TextBlock HorizontalAlignment TextEignment:

    <Style TargetType="{x:Type TextBlock}"> 
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="TextAlignment" Value="Right" /> 
        <Style.Triggers> 
            <DataTrigger Binding="{Binding Path=PrivateMemorySize, 
                        Converter={StaticResource isLarge},  
                        ConverterParameter=20000000}" Value="true"> 
                <Setter Property="Background" Value="Yellow" /> 
            </DataTrigger> 
        </Style.Triggers> 
    </Style>
+2

Source: https://habr.com/ru/post/1743536/


All Articles