Change Color of Single Letter in Label String?

13,626

Solution 1

Label is a content control so any type of content is permitted inside a label.You can easily do your requirement by something like

<Label>
    <StackPanel Orientation="Horizontal">
        <TextBlock Foreground="Red" Text="T"/>
        <TextBlock Text="ext"/>
    </StackPanel>
</Label>

Solution 2

A cleaner way would be using the flow-content-capabilites of a TextBlock:

<Label>
    <TextBlock>
        <Run Text="L" Foreground="Green"/>
        <Run Text="orem Ipsum"/>
    </TextBlock>
</Label>

This limits binding a bit though, if that is needed.

Solution 3

The cleanest method i found so far is using a TextEffect:

<Label>
    <TextBlock Text="Search">
        <TextBlock.TextEffects>
            <TextEffect PositionStart="0" PositionCount="1" Foreground="Red"/>
        </TextBlock.TextEffects>
    </TextBlock>
</Label>

This colors the "S" red. You can of course bind any of the involved properties if they need to be dynamic.

Solution 4

I just implemented something like this in our project, this will be static though - I'm not sure if that's what you need. You can change the content of the label as often as you need, but it will always have a red * at the end. I added a style to the project like this

<Style x:Key="RequiredFieldLabel"
       TargetType="{x:Type Label}">
  <Setter Property="ContentTemplate">
    <Setter.Value>
      <DataTemplate>
        <StackPanel Orientation="Horizontal">
          <TextBlock Text="{Binding}" />
          <TextBlock Text="*"
                   Foreground="red" />
        </StackPanel>
      </DataTemplate>
    </Setter.Value>
  </Setter>
</Style>

Then you can use this style on a label anywhere in your project.

<Label Content="Enter Name:"
       Style="{StaticResource RequiredFieldLabel}" />
Share:
13,626
CodeMouse92
Author by

CodeMouse92

Software Engineer at Canonical Lead Software Engineer at MousePaw Media Author of "Dead Simple Python" (No Starch Press, 2022) You can find many of my articles on DEV! As of 2018, I am largely inactive on StackOverflow due to the anti-social tendencies of the community as a whole, especially the high-reputation members, which results in harassment of beginners and marginalized groups. I largely blame the reputation system and no-accountability downvoting feature.

Updated on June 22, 2022

Comments

  • CodeMouse92
    CodeMouse92 almost 2 years

    I have a project in WPF 4 and VB.net. I need to change the color a single letter in a word in a label (the label's content changes quite a bit). I really am not sure if this is possible, but if it is, I'd appreciate help on figuring out how. TY!

  • Hiren Gondaliya
    Hiren Gondaliya over 12 years
    This is a better solution if your text needs to wrap
  • Ian
    Ian almost 11 years
    I'm looking to do something similar and I'm wondering if this technique will work and be bindable. For example highlighting all the uppercase letters in a dynamic string such as 'ThisIsMyTextBlock'. Would this be achievable using this approach?