Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Colors In TextBlock

Is it possible to add dynamic colors to a TextBlock ..i.e. have one character in one color and the next in another 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 input a dynamic 4 character sequence from no. I've bound it to this TextBlock inside a ListBox. Is it possible to have the characters in different colors.

If so is it possible to add these colors dynamically for eg. If I click a button certain characters change color?

Thank You. Any Help is appreciated.

like image 777
cjds Avatar asked Dec 03 '11 04:12

cjds


People also ask

Is TextBlock editable?

TextBlocks can be edited by users using the TextEditingTool. The HTMLInfo that a given TextBlock uses as its text editor can be customized by setting the textEditor property.

What is a TextBlock?

Text block is the primary control for displaying read-only text in apps. You can use it to display single-line or multi-line text, inline hyperlinks, and text with formatting like bold, italic, or underlined.

What is run in WPF?

Run method of Application class in WPF is used to starts an application. The code snippet in Listing 1 creates an Application instance and calls Run method. Application app = new Application (); app.Run();


1 Answers

Actually, you can, which can come in handy when you're doing a StringFormat on a data bound Textblock or a number of other places.

If you did want to try it though, like here's an SL example for a form label that puts a red asterisk next to the text Required Fields, but then can also add more stuff 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>
like image 172
Chris W. Avatar answered Sep 18 '22 03:09

Chris W.