我在文本块中有一行文本,内容如下:
“以 [accuracy] 的准确度检测到 [gesture]”
在 WPF 中,我是否可以更改文本块中元素的颜色?我可以让文本块有多种颜色吗?例如,我希望整个 TextBlock 是黑色的,除了手势名称,我希望它是红色的。
这在 WPF 中可能吗?
看看这是否有帮助:
<TextBlock>
Detected
<TextBlock Text="{Binding Gesture}" Foreground="Red" />
with an accuracy of
<TextBlock Text="{Binding Accuracy}" />
</TextBlock>
我认为您的意思是这样的(不是文本块的样式):
<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>
注意:运行标记之间的每个输入(或新行)都在它们之间留有空格。
我知道这篇文章很旧,但你试过吗?您实际上可以在 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 });
}
}
您可以为此使用RichTextBox并设置IsReadOnly = true