Multiple Colors In TextBlock

无人久伴 提交于 2019-11-29 04:28:41

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>

The TextBlock does not support multiple Foreground colors.

You could recreate this behaviour by using multiple textblocks (one for each letter) and placing them within a wrappanel. You could then change the color of individual characters/letters as you wish.
Beware of the probable performance impact this may have. The margins around individual letters will need to be adjusted to recreate standard behaviour though. Be especially careful around punctuation.

I'm developing for Mango with the WP7 SDK. You can use a <Run>. It seems a little buggy on WP7 , you have to add a spaces on the Run.Text property to get the spacing correct:

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

to set foreground color dynamically to a textblock

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!