[英]Can I have multiple colors in a single TextBlock in WPF?
我在文本塊中有一行文本,內容如下:
“以 [准確度] 的准確度檢測到 [手勢]”
在 WPF 中,我是否可以更改文本塊中元素的顏色? 我可以有多種顏色的文本塊嗎? 例如,除了我希望為紅色的手勢名稱外,我希望整個 TextBlock 為黑色。
這在 WPF 中可能嗎?
看看這是否有幫助:
<TextBlock>
Detected
<TextBlock Text="{Binding Gesture}" Foreground="Red" />
with an accuracy of
<TextBlock Text="{Binding Accuracy}" />
</TextBlock>
您可以為此使用RichTextBox並設置IsReadOnly = true
我知道這個帖子很舊,但是你試過這個嗎?? 您實際上可以在 TextBlock 中以這種方式添加多色文本。
Xaml: <TextBlock x:Name="txt_Txt"/>
foreach (var itm5 in "! Hello World !; %Hello World%".Split(';'))
{
if (txt_Txt.Inlines.Count() > 0)
txt_Txt.Inlines.Add(new Run("\r\n"));
foreach (var letter in itm5)
{
if (char.IsSymbol(letter))
txt_Txt.Inlines.Add(new Run(letter.ToString()) { Foreground = Brushes.Red });
else
txt_Txt.Inlines.Add(new Run(letter.ToString()) { Foreground = Brushes.Black });
}
}
我想你的意思是這樣的(不是文本塊的樣式):
<TextBlock FontSize="25" >
<Run Text="Detected [" Foreground="Red"/><Run Text="gesture" Foreground="Gray"/>
<Run Text="] with an accuracy of [" Foreground="Yellow"/><Run Text="accuracy]" Foreground="Green"/>
</TextBlock>
注意:Run 標記之間的每個輸入(或換行)都使它們之間有空格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.