WPF中的TextBlock

                                              WPF中的TextBlock

                                                                                      周银辉

 

TextBlock的一个很有趣的功能是其支持设置部分文本的样式,like this:

  textBlock.PNG

示例中的粗体、斜体等叫做
Inline Element,他们分别AnchoredBlock, Bold, Hyperlink, InlineUIContainer, Italic, LineBreak, Run, Span, Underline类型

 

要达到示例中的效果需要注意到TextBlock.Inlines属性,其为InlineCollection类型,通过它可以向TextBlock中添加Inline Element

比如我们向textBlock1中添加一个粗体的“TextBlock”字符串,则

C# 

 textBlock1.Inlines.Add( new  Bold( new  Run( " TextBlock " )));

            其中的Run表示没有格式化的普通文本

Xaml

< TextBlock  Name ="textBlock1"   >
    
< Bold > TextBlock </ Bold >  
</ TextBlock >


其他样式同理。

你可能感兴趣的:(block)