TextBlock 中的多种颜色 [英] Multiple Colors In TextBlock
问题描述
是否可以将动态颜色添加到 TextBlock ..i.e.一个字符用一种颜色,下一个用另一种颜色.
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>
基本上我从 no 输入了一个动态的 4 个字符序列.我已将其绑定到 ListBox 中的此 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.
推荐答案
实际上,您可以,当您在数据绑定 Textblock 或许多其他地方执行 StringFormat 时,这会派上用场.
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.
如果您确实想尝试一下,就像这里是表单标签的 SL 示例,它在文本必填字段"旁边放置一个红色星号,但也可以向其中添加更多内容,如示例所示.应该适用于 Silverlight、WPF、UWP 等...
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>
这篇关于TextBlock 中的多种颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!