2013-12-08 55 views

回答

4

在XAML中,定義了一個TextBlock包含多個Run元素用不同的顏色,就像這樣:

<TextBlock> 
    <Run Text="S"> 
     <Run.Foreground> 
      <SolidColorBrush Color="#DEFF6767" /> 
     </Run.Foreground> 
    </Run> 
    <Run Text="a"> 
     <Run.Foreground> 
      <SolidColorBrush Color="#DEDCFF00" /> 
     </Run.Foreground> 
    </Run> 
    <Run Text="m"> 
     <Run.Foreground> 
      <SolidColorBrush Color="#DE002EFF" /> 
     </Run.Foreground> 
    </Run> 
    <Run Text="p"> 
     <Run.Foreground> 
      <SolidColorBrush Color="#DEFF00D1" /> 
     </Run.Foreground> 
    </Run> 
    <Run Text="l"> 
     <Run.Foreground> 
      <SolidColorBrush Color="#DE97FF00" /> 
     </Run.Foreground> 
    </Run> 
    <Run Text="e"> 
     <Run.Foreground> 
      <SolidColorBrush Color="#DEB507D1" /> 
     </Run.Foreground> 
    </Run> 
</TextBlock> 

另外,這裏是你在C#中怎麼做:

TextBlock textBlock = new TextBlock(); 
textBlock.Inlines.Add(new Run { Text = "S", Foreground = new SolidColorBrush(Colors.Red) }); 
textBlock.Inlines.Add(new Run { Text = "a", Foreground = new SolidColorBrush(Colors.Blue) }); 
textBlock.Inlines.Add(new Run { Text = "m", Foreground = new SolidColorBrush(Colors.Yellow) }); 
textBlock.Inlines.Add(new Run { Text = "p", Foreground = new SolidColorBrush(Colors.Orange) }); 
textBlock.Inlines.Add(new Run { Text = "l", Foreground = new SolidColorBrush(Colors.Gray) }); 
textBlock.Inlines.Add(new Run { Text = "e", Foreground = new SolidColorBrush(Colors.Green) }); 
相關問題