fyodorfranz
fyodorfranz

Reputation: 486

Bind foreground attribute for child text of textblock element

I've discovered that I cannot set the foreground of text that is contained within a textblock as a child (as opposed to the text being set via the Text attribute on the textblock), by simply using the Foreground attribute on the parent textblock. I've tried setting the attributes TextElement.Foreground and Run.Foreground, but neither work. Does anyone know the preferred method in this case?

Here is my code thus far:

    <TextBlock Foreground="{Binding ForegroundColor}">
          Sample<LineBreak/>Text
    </TextBlock>

edit: Here's an interesting twist: if I take the <LineBreak/> out, the foreground sets just fine

Upvotes: 1

Views: 151

Answers (1)

mm8
mm8

Reputation: 169340

Make sure that you don't have an implicit Run style defined somewhere in your application:

<Style TargetType="Run">
     <Setter Property="Foreground" Value="Gray" />
</Style>

This one will override the Foreground setting of the parent TextBlock.

If you do you could add an implicit Run style to the TextBlock:

<TextBlock>
     <TextBlock.Resources>
         <Style TargetType="Run">
             <Setter Property="Foreground" Value="Orange" />
         </Style>
     </TextBlock.Resources>
    Sample<LineBreak/>Text
</TextBlock>

Upvotes: 2

Related Questions