Note that there are some explanatory texts on larger screens.

plurals
  1. POStyling a Textblock autogenerated in a ContentPresenter
    primarykey
    data
    text
    <p>As I saw, a lot of people ran into this exact problem but I can't understand why my case is not working and it is starting to drive me crazy.</p> <p>Context: I have a <code>DataGrid</code> which is to be colored according to the values of each cell. Hence, I have a dynamic style resolving the actual template to be used for each cell. Backgrounds now work accordingly.</p> <p>New problem: when I have a dark background, I want the font color to be white and the font weight to be bold so the text is correctly readable. And... I can't style it correctly.</p> <p>I read some Stackoverflow posts about that:</p> <p><a href="https://stackoverflow.com/questions/4739530/how-to-change-textblock-default-properties-for-a-contentpresenter-in-a-template">This one fits my problem but doesn't provide me any working solution</a> <a href="https://stackoverflow.com/questions/3063264/contentpresenter-within-controltemplate-cannot-change-attached-dependency-propert">This one is also clear and detail but... duh</a> <a href="https://stackoverflow.com/questions/3970285/wpf-4-contentpresenter-textwrapping-style-is-not-applied-to-implicitedly-generate">This is almost the same problem as me but... Solution does not work</a></p> <p>Here is what I tried so far:</p> <pre><code>&lt;!-- Green template--&gt; &lt;ControlTemplate x:Key="Green" TargetType="{x:Type tk:DataGridCell}"&gt; &lt;Grid Background="Green"&gt; &lt;ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"&gt; &lt;ContentPresenter.Resources&gt; &lt;Style BasedOn="{StaticResource BoldCellStyle}" TargetType="{x:Type TextBlock}" /&gt; &lt;/ContentPresenter.Resources&gt; &lt;/ContentPresenter&gt; &lt;/Grid&gt; &lt;/ControlTemplate&gt; </code></pre> <p>Does not work. Background is green, but text stays in black &amp; not bold.</p> <p>BTW, the BoldCellStyle is as easy as it can be:</p> <pre><code>&lt;Style x:Key="BoldCellStyle" TargetType="{x:Type TextBlock}"&gt; &lt;Setter Property="FontWeight" Value="Bold"/&gt; &lt;Setter Property="Foreground" Value="White" /&gt; &lt;/Style&gt; </code></pre> <p>Okay. Second try (which is a real stupid one but well...)</p> <pre><code>&lt;!-- Green template --&gt; &lt;ControlTemplate x:Key="Green" TargetType="{x:Type tk:DataGridCell}"&gt; &lt;Grid Background="Green"&gt; &lt;ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"&gt; &lt;ContentPresenter.Resources&gt; &lt;Style x:Key="BoldCellStyle" TargetType="{x:Type TextBlock}"&gt; &lt;Setter Property="FontWeight" Value="Bold"/&gt; &lt;Setter Property="Foreground" Value="White" /&gt; &lt;/Style&gt; &lt;/ContentPresenter.Resources&gt; &lt;/ContentPresenter&gt; &lt;/Grid&gt; &lt;/ControlTemplate&gt; </code></pre> <p>Doesn't work either.</p> <p>Then, I tried to play with the <code>ContentPresenter</code>'s properties:</p> <pre><code>&lt;!-- Green template --&gt; &lt;ControlTemplate x:Key="Green" TargetType="{x:Type tk:DataGridCell}"&gt; &lt;Grid Background="Green"&gt; &lt;ContentPresenter TextElement.FontWeight="Bold" TextElement.Foreground="White" TextBlock.Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" /&gt; &lt;/Grid&gt; &lt;/ControlTemplate&gt; </code></pre> <p>And... As you can expect, this does not even work.</p> <p>Intrigued, I used Snoop to browse all the components of my interface. In the first two cases, Snoop actually shows me that each cell is a <code>Grid</code> with a <code>ContentPresenter</code> containing a <code>TextBlock</code> and the actual <code>Style</code> but... The <code>TextBlock</code>'s properties do not apply and <code>FontWeight</code> is still normal.</p> <p>Last case, even more shocking, I can see that snoop shows me that we actually have a <code>ContentPresenter</code> with the right properties (ie <code>TextElement.FontWeight="Bold"</code>), but the autogenerated <code>TextBlock</code> under is - still - not styled.</p> <p>I can't get what am I missing here. I tried as you can see almost all I could possibly do here, and the <code>TextBlock</code>s keep being non-formatted.</p> <p>Any idea here? Thanks again!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload