WPF Binding 中使用 StringFormat

TextBlock 中 Text 格式化

TextBlock 的 Text 可以绑定多个数据源,配合 StringFormat 就可以很方便地显示了。

 1 <TextBlock Text="{Binding Text,StringFormat=Name:{0}}"/>
 2 <TextBlock FontSize="30" TextWrapping="Wrap">
 3     <TextBlock.Text>
 4         <MultiBinding StringFormat="Name:&#x0a;   {0}&#x0a;&#x0a;Age:&#x0a;   {1}&#x0a;&#x0a;Grade:&#x0a;   {2}">
 5             <Binding Path="Name"/>
 6             <Binding Path="Age"/>
 7             <Binding Path="Grade"/>
 8         </MultiBinding>
 9     </TextBlock.Text>
10 </TextBlock>

需要注意的是,StringFormat="{0}{1}"会报错,可以加上一个空{},即StringFormat="{}{0}{1}"。

XAML 中的特殊字符

\a  &#x07;   BEL
\b  &#x08;    BS - Backspace
\f  &#x0c;    FF - Formfeed
\n  &#x0a;    LF, NL - Linefeed, New Line
\r  &#x0d;    CR - Carriage return
\t  &#x09;    HT - Tab, Horizontal Tabelator
\v  &#x0b;    VT - Vertical Tabelator

 

如果同时设置 Converter 和 StringFormat 属性,则会先对数据值应用转换器,然后再应用 StringFormat。

 

本文参考:http://www.cnblogs.com/xiwang/archive/2013/05/26/3100009.html

 

posted @ 2014-12-12 11:52  fujinxiang  阅读(754)  评论(0编辑  收藏  举报