Styling a text block created in ContentPresenter

As I saw, many people faced this exact problem, but I can’t understand why my case does not work, and it starts to drive me crazy.

Context: I have DataGridone that needs to be colored according to the values ​​of each cell. Therefore, I have a dynamic style allowing the actual template to be used for each cell. Backgrounds now work accordingly.

New problem: when I have a dark background, I want the font color to be white and the font to be bold so that the text is legible. And ... I can't fine tune it.

I read several Stackoverflow posts about this:

This is consistent with my problem, but does not give me any working solution. It is also clear and detailed, but ... duh This is almost the same problem as me, but ... The solution does not work

Here is what I have tried so far:

<!-- Green template-->
    <ControlTemplate x:Key="Green" TargetType="{x:Type tk:DataGridCell}">
        <Grid Background="Green">
            <ContentPresenter
                            HorizontalAlignment="Center"
                                      VerticalAlignment="Center">
                <ContentPresenter.Resources>
                    <Style BasedOn="{StaticResource BoldCellStyle}" TargetType="{x:Type TextBlock}" />
                </ContentPresenter.Resources>
            </ContentPresenter>
        </Grid>
    </ControlTemplate>

Does not work. The background is green, but the text remains black and not bold.

BTW, BoldCellStyle is as simple as it can be:

<Style x:Key="BoldCellStyle" TargetType="{x:Type TextBlock}">
    <Setter Property="FontWeight" Value="Bold"/>
    <Setter Property="Foreground" Value="White" />
</Style>

Good. Second attempt (this is really stupid, but good ...)

<!-- Green template  -->
    <ControlTemplate x:Key="Green" TargetType="{x:Type tk:DataGridCell}">
        <Grid Background="Green">
            <ContentPresenter
                            HorizontalAlignment="Center"
                                      VerticalAlignment="Center">
                <ContentPresenter.Resources>
                    <Style x:Key="BoldCellStyle" TargetType="{x:Type TextBlock}">
                        <Setter Property="FontWeight" Value="Bold"/>
                        <Setter Property="Foreground" Value="White" />
                    </Style>

                </ContentPresenter.Resources>
            </ContentPresenter>
        </Grid>
    </ControlTemplate>

Does not work.

Then I tried to reproduce the properties ContentPresenter:

<!-- Green template -->
<ControlTemplate x:Key="Green" TargetType="{x:Type tk:DataGridCell}">
    <Grid Background="Green">
        <ContentPresenter TextElement.FontWeight="Bold" TextElement.Foreground="White" TextBlock.Foreground="White"
                        HorizontalAlignment="Center"
                                  VerticalAlignment="Center" />
    </Grid>
</ControlTemplate>

And ... As you would expect, this doesn't even work.

, Snoop . Snoop , Grid ContentPresenter, a TextBlock Style, ... TextBlock , FontWeight - .

, , , snoop , ContentPresenter (.. TextElement.FontWeight="Bold"), TextBlock - - .

, . , , , TextBlock .

? !

+5
1

DataGridColumns, DataGridBoundColumn ( DataGridTemplateColumn), ElementStyle, TextBlock . . DataGridTextColumn

static DataGridTextColumn()
{
    ElementStyleProperty.OverrideMetadata(typeof(DataGridTextColumn),
        new FrameworkPropertyMetadata(DefaultElementStyle));
    // ...
}

ElementStyle , DefaultElementStyle, TextBlock.

public static Style DefaultElementStyle
{
    get
    {
        if (_defaultElementStyle == null)
        {
            Style style = new Style(typeof(TextBlock));
            // Use the same margin used on the TextBox to provide space for the caret
            style.Setters.Add(new Setter(TextBlock.MarginProperty, new Thickness(2.0, 0.0, 2.0, 0.0)));
            style.Seal();
            _defaultElementStyle = style;
        }
        return _defaultElementStyle;
    }
}

, DataGridCell element.Style = style;, , , .

,

<DataGridTextColumn Header="Column 1" ElementStyle="{StaticResource BoldCellStyle}" .../>
<DataGridTextColumn Header="Column 2" ElementStyle="{StaticResource BoldCellStyle}" .../>
+2

All Articles