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:
<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 ...)
<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:
<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 .
? !