在Silverlight项目中,经常会根据需求的不同,提供不同的字符格式到客户端。这篇文章总结了目前Silverlight 4常用StringFormat绑定字符格式,方便大家在以后的开发中调用参考。
早期的格式转换方法
在Silverlight 4之前的版本,通常修改字符格式是通过实现IValueConverter接口,在IValueConverter接口中包含有两个方法:
1. Convert, 在绑定源数据传送到客户端显示前,转换修改数据;
2. ConvertBack,在目标数据传送到绑定源数据前,转换修改数据,该方法仅被用于“TwoWay”绑定模式;
例如,根据布尔型判断实现控件是否显示在客户端,下面是一个简单的转换代码,通过实现IValueConverter,判断控件Visibility属性值。
1
public
class
VisibilityConverter : IValueConverter
2
3
{
4
5
public
object
Convert(
object
value,Type targetType,
object
parameter,CultureInfo culture)
6
7
{
8
9
bool
visibility
=
(
bool
)value;
10
11
return
visibility
?
Visibility.Visible : Visibility.Collapsed;
12
13
}
14
15
public
object
ConvertBack(
object
value,Type targetType,
object
parameter,CultureInfo culture)
16
17
{
18
19
Visibility visibility
=
(Visibility)value;
20
21
return
(visibility
==
Visibility.Visible);
22
23
}
24
25
}
Silverlight 4的StringFormat属性
在Silverlight 4中,数据绑定SDK提供一个“StringFormat”的属性,该属性允许开发人员自由定义输出字符格式,例如,DataPicker控件,该控件提供SelectedDateFormat属性,控制其输出格式“短格式”或“长格式”。
SelectedDateFormat="Short", “默认”
SelectedDateFormat="Long"
而如果需要输出“April 01, 2011”格式,则无法使用SelectedDateFormat控制其格式,这时可以使用StringFormat属性完成需求。
再例如,早期实现一段文字绑定信息需要按照以下格式操作:
输出: 当前登录用户Jv9
在Silverlight 4中使用StringFormat属性,可以简单的实现如下:
这样实现简单而又方便。
对于StringFormat属性的使用方法还有很多,下面使用实例演示常用的属性:
2 < ScrollViewer x:Name ="PageScrollViewer" Style ="{StaticResource PageScrollViewerStyle}" >
3 < StackPanel Orientation ="Horizontal" >
4 < StackPanel x:Name ="ContentStackPanel" >
5 < TextBlock FontSize ="16" TextWrapping ="Wrap" Text ="StringFormat和字符串格式" Margin ="8" />
6 < Border BorderBrush ="LightGray" BorderThickness ="1" Width ="300" Margin ="5" HorizontalAlignment ="Left" >
7 < StackPanel Orientation ="Vertical" Margin ="5" >
8 < TextBox x:Name ="txtSource" Width ="125" HorizontalAlignment ="Left" />
9 < TextBlock TextWrapping ="Wrap" Text ="StringFormat='正在输入 "\{0\}"." FontWeight ="Bold" Margin ="0,4,0,2" />
10 < TextBlock Text ="{Binding Text, ElementName=txtSource, StringFormat='正在输入 "\{0\}".'}" TextWrapping ="Wrap" Margin ="0,0,0,4" />
11 < TextBlock TextWrapping ="Wrap" Text ="StringFormat=\{0\,20\}" FontWeight ="Bold" Margin ="0,4,0,2" />
12 < TextBlock Text ="{Binding Text, ElementName=txtSource, StringFormat=\{0\,20\}}" TextWrapping ="Wrap" Margin ="0,0,0,4" />
13 < TextBlock TextWrapping ="Wrap" Text ="StringFormat=\{0\,-20\}" FontWeight ="Bold" Margin ="0,4,0,2" />
14 < TextBlock Text ="{Binding Text, ElementName=txtSource, StringFormat=\{0\,-20\}}" TextWrapping ="Wrap" Margin ="0,0,0,4" />
15 StackPanel >
16 Border >
17
18 < StackPanel x:Name ="DatePanel" >
19 < TextBlock Text ="StringFormat和日期格式" Margin ="8" FontSize ="16" />
20 < Border BorderBrush ="LightGray" BorderThickness ="1" Width ="300" Margin ="5" HorizontalAlignment ="Left" >
21 < StackPanel DataContext ="{StaticResource SampleDate}" >
22 < TextBlock TextWrapping ="Wrap" Text ="StringFormat=f" FontWeight ="Bold" Margin ="0,4,0,2" />
23 < TextBlock TextWrapping ="Wrap" Text ="{Binding CurrentDateTime, StringFormat=f}" Margin ="8,0" />
24 < TextBlock TextWrapping ="Wrap" Text ="StringFormat=F" FontWeight ="Bold" Margin ="0,4,0,2" />
25 < TextBlock TextWrapping ="Wrap" Text ="{Binding CurrentDateTime, StringFormat=F}" Margin ="8,0" />
26 < TextBlock TextWrapping ="Wrap" Text ="StringFormat=g" FontWeight ="Bold" Margin ="0,4,0,2" />
27 < TextBlock TextWrapping ="Wrap" Text ="{Binding CurrentDateTime, StringFormat=g}" Margin ="8,0" />
28 < TextBlock TextWrapping ="Wrap" Text ="StringFormat=G" FontWeight ="Bold" Margin ="0,4,0,2" />
29 < TextBlock TextWrapping ="Wrap" Text ="{Binding CurrentDateTime, StringFormat=G}" Margin ="8,0" />
30 < TextBlock TextWrapping ="Wrap" Text ="StringFormat=m" FontWeight ="Bold" Margin ="0,4,0,2" />
31 < TextBlock TextWrapping ="Wrap" Text ="{Binding CurrentDateTime, StringFormat=m}" Margin ="8,0" />
32 < TextBlock TextWrapping ="Wrap" Text ="StringFormat=y" FontWeight ="Bold" Margin ="0,4,0,2" />
33 < TextBlock TextWrapping ="Wrap" Text ="{Binding CurrentDateTime, StringFormat=y}" Margin ="8,0" />
34 < TextBlock TextWrapping ="Wrap" Text ="StringFormat=t" FontWeight ="Bold" Margin ="0,4,0,2" />
35 < TextBlock TextWrapping ="Wrap" Text ="{Binding CurrentDateTime, StringFormat=t}" Margin ="8,0" />
36 < TextBlock TextWrapping ="Wrap" Text ="StringFormat=T" FontWeight ="Bold" Margin ="0,4,0,2" />
37 < TextBlock TextWrapping ="Wrap" Text ="{Binding CurrentDateTime, StringFormat=T}" Margin ="8,0" />
38 < TextBlock TextWrapping ="Wrap" Text ="StringFormat=u" FontWeight ="Bold" Margin ="0,4,0,2" />
39 < TextBlock TextWrapping ="Wrap" Text ="{Binding CurrentDateTime, StringFormat=u}" Margin ="8,0" />
40 < TextBlock TextWrapping ="Wrap" Text ="StringFormat=o" FontWeight ="Bold" Margin ="0,4,0,2" />
41 < TextBlock TextWrapping ="Wrap" Text ="{Binding CurrentDateTime, StringFormat=o}" Margin ="8,0" />
42 < TextBlock TextWrapping ="Wrap" Text ="StringFormat=MM/dd/yy" FontWeight ="Bold" Margin ="0,4,0,2" />
43 < TextBlock TextWrapping ="Wrap" Text ="{Binding CurrentDateTime, StringFormat=MM/dd/yy}" Margin ="8,0" />
44 < TextBlock TextWrapping ="Wrap" Text ="StringFormat=MMMM dd\, yyyy g" FontWeight ="Bold" Margin ="0,4,0,2" />
45 < TextBlock TextWrapping ="Wrap" Text ="{Binding CurrentDateTime, StringFormat=MMMM dd\, yyyy g}" Margin ="8,0" />
46 < TextBlock TextWrapping ="Wrap" Text ="StringFormat=hh:mm:ss.fff tt" FontWeight ="Bold" Margin ="0,4,0,2" />
47 < TextBlock TextWrapping ="Wrap" Text ="{Binding CurrentDateTime, StringFormat=hh:mm:ss.fff tt}" Margin ="8,0" />
48 < TextBlock TextWrapping ="Wrap" Text ="StringFormat=\{0:r\}" FontWeight ="Bold" Margin ="0,4,0,2" />
49 < TextBlock TextWrapping ="Wrap" Text ="{Binding CurrentDateTime, StringFormat=\{0:r\}}" Margin ="8,0" />
50 < TextBlock TextWrapping ="Wrap" Text ="StringFormat='MMM d, yyyy'" FontWeight ="Bold" Margin ="0,4,0,2" />
51 < TextBlock TextWrapping ="Wrap" Text ="{Binding CurrentDateTime, StringFormat='MMM d, yyyy'}" Margin ="8,0" />
52 StackPanel >
53 Border >
54 StackPanel >
55 StackPanel >
由于51cto博文字数限制这里只能贴出部分源代码,更多代码请下载源代码。
源代码下载
在线演示
欢迎大家加入“专注Silverlight”QQ技术群,欢迎大家加入一起学习讨论Silverlight&WPF&Widnows Phone开发技术。
22308706(一群) 超级群500人
37891947(二群) 超级群500人
100844510(三群) 高级群200人
32679922(四群) 超级群500人
23413513(五群) 高级群200人
32679955(六群) 超级群500人
61267622(七群) 超级群500人
88585140(八群) 超级群500人
128043302(九群 企业应用开发推荐群) 高级群200人
101364438(十群) 超级群500人
68435160(十一群 企业应用开发推荐群)超级群500人