TextBlock中的多种颜色

cjd*_*jds 7 xaml textblock text-coloring windows-phone-7

是否可以为TextBlock添加动态颜色..我有一个颜色的一个字符和另一个颜色的下一个字符.

<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>
Run Code Online (Sandbox Code Playgroud)

基本上我从no输入动态4字符序列.我将它绑定到ListBox中的TextBlock.是否可以使用不同颜色的字符.

如果是这样,可以动态添加这些颜色,例如.如果我点击按钮,某些字符会改变颜色?

谢谢.任何帮助表示赞赏.

Chr*_* W. 27

实际上,你可以在数据绑定Textblock或许多其他地方进行StringFormat时派上用场.

如果您确实想尝试它,就像这里是一个表单标签的SL示例,它在文本必填字段旁边放置一个红色星号,但是也可以添加更多内容,如示例所示.应该适用于Silverlight,WPF,UWP等......

<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>
Run Code Online (Sandbox Code Playgroud)