WPF中讓TextBlock每一個字符顯示不同的顏色


XAML代碼:

<TextBlock x:Name="tb">
              <Run Foreground="Red">R</Run>
              <Run Foreground="Green">G</Run>
              <Run Foreground="Blue">B</Run>
              <Run Text="Gradient">
                 <Run.Foreground>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                       <GradientStop Color="#FF000000" Offset="0"/>
                       <GradientStop Color="#FFFFFFFF" Offset="1"/>
                   </LinearGradientBrush>
              </Run.Foreground>
         </Run>
</TextBlock>

在后台的邏輯代碼中應訪問TextBlock的Inlines集合來得到它所包含的字串..如果你用Text屬性是取不到值的..

CS代碼:

string str1 = this.tb.Text;
string str2 = "";
foreach (Run r in tb.Inlines)
{
    str2 += r.Text;
}
MessageBox.Show("str1:["+str1+"]         str2:["+str2+"]");

運行結果:

TextBlock的顯示效果:圖片

顯示TextBlock內的字符串:
圖片


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM