设置 TextBlock 中文本的格式
如何在 WPF 应用程序中的 TextBlock
控件内实现文本格式设置?
例如:我希望某些单词以粗体显示,其他单词以斜体显示,还有一些单词以不同的颜色显示,例如:
原因是这个实际问题:
lblcolorfrom.Content = "Colour From: " + colourChange.ElementAt(3).Value.ToUpper();
我希望字符串的第二部分加粗,并且我知道我可以使用两个控件(标签、文本块等),但我不想,由于已经使用了大量的控件。
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();
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要使用
内联
:使用绑定:
您还可以绑定其他属性:
如果您将粗体作为布尔值(例如),则可以通过转换器进行绑定。
You need to use
Inlines
:With binding:
You can also bind the other properties:
You can bind through converters if you have bold as a boolean (say).
您可以在 XAML 中轻松完成此操作:
You can do this in XAML easily enough:
有各种
内联
元素可以帮助您,对于最简单的格式选项,您可以使用粗体
、斜体< /code>
和
下划线
:我认为值得注意的是,这些元素实际上只是
Span
元素设置了各种属性(即:Bold
,FontWeight
属性设置为FontWeights.Bold
)。这给我们带来了下一个选择:前面提到的
Span
元素。您可以使用此元素实现与上述相同的效果,但您可以获得更多的可能性;您可以设置(除其他外)
Foreground
或Background
属性:Span
元素还可能包含其他元素,如下所示:还有一个元素,与
Span
非常相似,它称为运行
。Run
不能包含其他内联元素,而Span
可以,但您可以轻松地将变量绑定到Run
的Text< /code> property:
另外,如果您愿意,您可以从代码隐藏中执行整个格式化:
There are various
Inline
elements that can help you, for the simplest formatting options you can useBold
,Italic
andUnderline
:I think it is worth noting, that those elements are in fact just shorthands for
Span
elements with various properties set (i.e.: forBold
, theFontWeight
property is set toFontWeights.Bold
).This brings us to our next option: the aforementioned
Span
element.You can achieve the same effects with this element as above, but you are granted even more possibilities; you can set (among others) the
Foreground
or theBackground
properties:The
Span
element may also contain other elements like this:There is another element, which is quite similar to
Span
, it is calledRun
. TheRun
cannot contain other inline elements while theSpan
can, but you can easily bind a variable to theRun
'sText
property:Also, you can do the whole formatting from code-behind if you prefer:
查看 Charles Petzolds Bool 应用程序 = 代码 + 标记的示例
Check out this example from Charles Petzolds Bool Application = Code + markup
一个很好的网站,有很好的解释:
http:// www.wpf-tutorial.com/basic-controls/the-textblock-control-inline-formatting/
这里作者为您提供了您正在寻找的内容的好例子!总的来说,该网站非常适合研究材料,而且它涵盖了 WPF 中的大量选项
编辑
有多种不同的方法来设置文本格式。对于基本格式(我认为最简单):
示例 1 显示了使用粗体斜体和下划线文本的基本格式。
以下包括 SPAN 方法,您可以使用它突出显示文本:
示例 2 显示了 span 函数及其不同的可能性。
详细的解释请查看网站!
示例
a good site, with good explanations:
http://www.wpf-tutorial.com/basic-controls/the-textblock-control-inline-formatting/
here the author gives you good examples for what you are looking for! Overal the site is great for research material plus it covers a great deal of options you have in WPF
Edit
There are different methods to format the text. for a basic formatting (the easiest in my opinion):
Example 1 shows basic formatting with Bold Itallic and underscored text.
Following includes the SPAN method, with this you van highlight text:
Example 2 shows the span function and the different possibilities with it.
For a detailed explanation check the site!
Examples
这是我的解决方案......
我正在学习......所以如果有人对上述解决方案有想法,请分享! :)
This is my solution....
I am learning... so if anyone has thaughts on the above solution please share! :)