SilverLight 5 数据绑定

字符串格式

  Bindings有一个StringFormat属性可以用于在数据传递到目标控件之前格式化被绑定属性的值。这是一个很有用的功能,特别是绑定到非字符串属性时,如DateTime或Decimal类型。可以使用StringFormat属性来设置格式 ,使用与在代码中ToString方法相同的格式化字符串值的方法,即可以使用标准格式也可以定制格式。比如,如下绑定使用标准格式显示被绑定属性的值在TextBox中,将文件格式为现金格式:

<TextBox Text=”{Binding TotalCost,Mode=TwoWay,stringFormat=C}” />

  或者你可以将日期格式化为长日期格式 (如Sunday,May 16,2011):

<TextBox Text="{Binding StartDate, Mode=TwoWay, StringFomat=D}” />

  或者使用定制的格式(如2011-05-16):

<TextBox Text="{Binding StartDate, Mode=TwoWay, StringFormat=yyyy-MM-dd}"/>

  也可以格式化decimal,singl,double或float为一种给定小数位数的格式(如3.14):

<TextBox Text="{Binding PI, Mode=TwoWay, StringFormat=0.00}"/>
posted @ 2013-03-06 13:09  银河系上的地球  阅读(173)  评论(0编辑  收藏  举报