Label

1
2
3
4
5
6
7
8
<Label Content="{Binding Model.Progress,Mode=OneWay}" ContentStringFormat="aasdf{0:P}asdfasdf"/>-->
<!--<TextBlock Text="{Binding Model.MyProperty, StringFormat={}{0:F4}}"/>
<TextBlock Text="{Binding Model.MyProperty, StringFormat={}{0:P1}}" />
<Label>
<Label.Content>
<Binding Path="Model.Progress" StringFormat="{}{0:N}"/>
</Label.Content>
</Label>

TextBlock

货币格式

<TextBlock Text="&#123;Binding Price, StringFormat=&#123;&#125;&#123;0:C&#125;&#125;" /> // $123.46

货币格式,一位小数

<TextBox Text="&#123;Binding Price, StringFormat=&#123;&#125;&#123;0:C1&#125;&#125;" /> // $123.5

前文字

<TextBox Text="&#123;Binding Price, StringFormat=单价:&#123;0:C&#125;&#125;" /> //单价:$123.46

后文字

<TextBox Text="&#123;Binding Price, StringFormat=&#123;&#125;&#123;0&#125;元&#125;" /> // 123.45678元

固定的位数,位数不能少于未格式化前,仅支持整形

<TextBox Text="&#123;Binding Count, StringFormat=&#123;&#125;&#123;0:D6&#125;&#125;" /> // 086723

指定小数点后的位数

<TextBox Tet="&#123;Binding Total, StringFormat=&#123;&#125;&#123;0:F4&#125;&#125;" /> // 28768234.9329

用分号隔开的数字,并指定小数点后的位数

<TextBox Text="&#123;Binding Total, StringFormat=&#123;&#125;&#123;0:N3&#125;&#125;" /> // 28,768,234.933

格式化百分比

<TextBox Text="&#123;Binding Persent, StringFormat=&#123;&#125;&#123;0:P1&#125;&#125;" /> // 78.9 %

占位符

<TextBox Text="&#123;Binding Price, StringFormat=&#123;&#125;&#123;0:0000.00&#125;&#125;" /> // 0123.46
<TextBox Text="&#123;Binding Price, StringFormat=&#123;&#125;&#123;0:####.##&#125;&#125;" /> // 123.46

日期/时间

<TextBox Text="&#123;Binding DateTimeNow, StringFormat=&#123;&#125;&#123;0:d&#125;&#125;" /> // 5/4/2015
<TextBox Text="&#123;Binding DateTimeNow, StringFormat=&#123;&#125;&#123;0:D&#125;&#125;" /> // Monday, May 04, 2015
<TextBox Text="&#123;Binding DateTimeNow, StringFormat=&#123;&#125;&#123;0:f&#125;&#125;" /> // Monday, May 04, 2015 5:46 PM
<TextBox Text="&#123;Binding DateTimeNow, StringFormat=&#123;&#125;&#123;0:F&#125;&#125;" /> // Monday, May 04, 2015 5:46:56 PM
<TextBox Text="&#123;Binding DateTimeNow, StringFormat=&#123;&#125;&#123;0:g&#125;&#125;" /> // 5/4/2015 5:46 PM
<TextBox Text="&#123;Binding DateTimeNow, StringFormat=&#123;&#125;&#123;0:G&#125;&#125;" /> // 5/4/2015 5:46:56 PM
<TextBox Text="&#123;Binding DateTimeNow, StringFormat=&#123;&#125;&#123;0:m&#125;&#125;" /> // May 04
<TextBox Text="&#123;Binding DateTimeNow, StringFormat=&#123;&#125;&#123;0:M&#125;&#125;" /> // May 04
<TextBox Text="&#123;Binding DateTimeNow, StringFormat=&#123;&#125;&#123;0:t&#125;&#125;" /> // 5:46 PM
<TextBox Text="&#123;Binding DateTimeNow, StringFormat=&#123;&#125;&#123;0:T&#125;&#125;" /> // 5:46:56 PM
<TextBox Text="&#123;Binding DateTimeNow, StringFormat=&#123;&#125;&#123;0:yyyy年MM月dd日&#125;&#125;" /> // 2015年05月04日
<TextBox Text="&#123;Binding DateTimeNow, StringFormat=&#123;&#125;&#123;0:yyyy-MM-dd&#125;&#125;" /> // 2015-05-04
<TextBox Text="&#123;Binding DateTimeNow, StringFormat=&#123;&#125;&#123;0:yyyy-MM-dd HH:mm&#125;&#125;" /> // 2015-05-04 17:46
<TextBox Text="&#123;Binding DateTimeNow, StringFormat=&#123;&#125;&#123;0:yyyy-MM-dd HH:mm:ss&#125;&#125;" /> // 2015-05-04 17:46:56

多重绑定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<TextBox.Text>
<MultiBinding StringFormat="姓名:{0}{1}">
<Binding Path="FristName" />
<Binding Path="LastName" />
</MultiBinding>
</TextBox.Text>
// 姓名:AAbb
多重绑定中的特殊字符
复制代码
<TextBox.Text>
<MultiBinding StringFormat="姓名:{0}&#x09;{1}">
<Binding Path="FristName" />
<Binding Path="LastName" />
</MultiBinding>
</TextBox.Text>
<!--
\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
-->