Multiple colors in TextBlock

Is it possible to add dynamic colors to TextBlock..ie have one character in one color, and the next in a different color.

<TextBlock Text="{Binding no}" TextWrapping="Wrap" Margin="10,0,0,0" Style="{StaticResource PhoneTextSubtleStyle}" FontSize="40" Foreground="#A400C4FF" > // Can we add something here to specify what colours for what chars </TextBlock> 

Basically I enter a dynamic sequence of 4 characters from no. I bound it to this TextBlock inside the ListBox. Is it possible to have characters of different colors.

If so, you can dynamically add these colors, for example. If I press a button, do some characters change color?

Thanks. Any help is appreciated.

+6
source share
4 answers

Actually, you can, which can come in handy when you do StringFormat in data-bound Textblock or in a number of other places.

If you really want to try, as here, the SL example is for a form label that places a red star next to the text "Required fields", but then can also add more things to it, as shown in the example. Should work for Silverlight, WPF, UWP, etc.

 <TextBlock> <Run Text="*" Foreground="#FFE10101"/><Run Text="Required Line" /> <Run Text="Red" Foreground="Red"/> <Run Text="Blue" Foreground="Blue"/> <Run Text="{Binding SomeString, StringFormat='Hell ya you can make \{0\} a different color!'}" Foreground="Orange"/> </TextBlock> 
+25
source

TextBlock does not support multiple foreground colors.

You can recreate this behavior by using several text blocks (one for each letter) and placing them in a shell. Then you can change the color of the individual characters / letters as you wish.
Beware of the potential performance impact this may have. Margins around individual letters will need to be adjusted to recreate standard behavior. Be especially careful with punctuation.

0
source

I am developing Mango with the WP7 SDK. You can use <Run>. It seems that WP7 is a bit buggy, you need to add spaces to the Run.Text property in order to set the interval correctly:

 <TextBlock>Hello<Run Foreground="Bisque" Text=" Holla "></Run>and hello again!</TextBlock>; 
0
source

to dynamically highlight foreground color in a text block

use: txtblockname.Foreground = new SolidColorBrush (Colors.Yellow);

0
source

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


All Articles