问题描述
我如何实现我的WPF应用程序中的的TextBlock
控件中的文本格式?
How do I achieve formatting of a text inside a TextBlock
control in my WPF application?
例如:我想有大胆某些词,别人斜体,还有一些不同的颜色,这样的例子:
e.g.: I would like to have certain words in bold, others in italic, and some in different colors, like this example:
背后我的问题的原因是这个实际的问题:
The reason behind my question is this actual problem:
lblcolorfrom.Content = "Colour From: " + colourChange.ElementAt(3).Value.ToUpper();
我想字符串的第二部分是勇敢的,我知道,我可以用两个控件(标签,的TextBlocks等),但我宁愿不要,因为控件的大量已被使用。
I would like the second part of the string to be bold, and I know that I could use two controls (Labels, TextBlocks, etc.) but I'd rather not, due the vast amount of controls already in use.
推荐答案
您需要使用<$c$c>Inlines$c$c>:
<TextBlock.Inlines>
<Run FontWeight="Bold" FontSize="14" Text="This is WPF TextBlock Example. " />
<Run FontStyle="Italic" Foreground="Red" Text="This is red text. " />
</TextBlock.Inlines>
通过绑定:
<TextBlock.Inlines>
<Run FontWeight="Bold" FontSize="14" Text="{Binding BoldText}" />
<Run FontStyle="Italic" Foreground="Red" Text="{Binding ItalicText}" />
</TextBlock.Inlines>
您还可以绑定其他属性:
You can also bind the other properties:
<TextBlock.Inlines>
<Run FontWeight="{Binding Weight}"
FontSize="{Binding Size}"
Text="{Binding LineOne}" />
<Run FontStyle="{Binding Style}"
Foreground="Binding Colour}"
Text="{Binding LineTwo}" />
</TextBlock.Inlines>
您可以通过转换器绑定,如果您有大胆的布尔(说)。
You can bind through converters if you have bold as a boolean (say).
这篇关于在TextBlock中设置文本格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!