Change the color of a single letter in a label string?

I have a project in WPF 4 and VB.net. I need to change the color with one letter in the word in the label (the contents of the labels change very little). I'm really not sure if this is possible, but if so, I would appreciate help in figuring out how to do this. TY!

+6
source share
4 answers

A shortcut is a content control, so any type of content is allowed inside the shortcut. You can easily fulfill your requirement with

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

A cleaner way would be to use the flow-content-capabilites for a TextBlock:

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

This limits bit binding, if necessary.

+13
source

The cleanest method I've found so far is using TextEffect :

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

The color "S" is red. Of course, you can bind any involved properties if they should be dynamic.

+6
source

I just implemented something similar in our project, it will be static, though - I'm not sure what you need. You can change the contents of the shortcut as often as you need, but it will always be red * at the end. I added style to a 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> 

You can then use this style on the label anywhere in your project.

 <Label Content="Enter Name:" Style="{StaticResource RequiredFieldLabel}" /> 
+2
source

Source: https://habr.com/ru/post/888722/


All Articles