silverlight   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Silverlight 4常用StringFormat格式总结大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

在Silverlight项目中,经常会根据需求的不同,提供不同的字符格式到客户端。这篇文章总结了目前Silverlight 4常用StringFormat绑定字符格式,方便大家在以后的开发中调用参。 早期的格式转换方法 在Silverlight 4之前的版本,通常修改字符格式是通过实现IValueConverter接口,在IValueConverter接口中包含有两个方法: 1. Convert

@H_301_7@

在Silverlight项目中,经常会根据需求的不同,提供不同的字符格式到客户端。这篇文章总结了目前Silverlight 4常用StringFormat绑定字符格式,方便大家在以后的开发中调用。@H_301_7@

早期的格式转换方法@H_301_7@

在Silverlight 4之前的版本,通常修改字符格式是通过实现IValueConverter接口,在IValueConverter接口中包含有两个方法:@H_301_7@

1. Convert, 在绑定源数据传送到客户端显示前,转换修改数据;@H_301_7@

2. ConvertBACk,在目标数据传送到绑定源数据前,转换修改数据,该方法仅被用于“TwoWay”绑定模式;@H_301_7@

例如,根据布尔型判断实现控件是否显示在客户端,下面是一个简单的转换代码,通过实现IValueConverter,判断控件Visibility属性值。@H_301_7@

 @H_301_7@

 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,CultureInfo culturE)
16 
17  {
18 
19  Visibility visibility  =  (Visibility)value;
20 
21  return  (visibility  ==  Visibility.VisiblE);
22 
23  }
24 
25  }

 @H_301_7@  

Silverlight 4的StringFormat属性@H_301_7@

在Silverlight 4中,数据绑定SDK提供一个StringFormat”的属性,该属性允许开发人员自由定义输出字符格式,例如,DataPicker控件,该控件提供SELEctedDateFormat属性,控制其输出格式“短格式”或“长格式”。@H_301_7@

SELEctedDateFormat="Short",“认”@H_301_7@

Silverlight 4常用StringFormat格式总结

@H_301_7@

SELEctedDateFormat="Long"@H_301_7@

Silverlight 4常用StringFormat格式总结

@H_301_7@

而如果需要输出“April 01,2011”格式,则无法使SELEctedDateFormat控制其格式,这时可以使用StringFormat属性完成需求。@H_301_7@

<toolkit:DatePicker SELEctedDate="{Binding DemoDate,Mode=TwoWay,StringFormat='MMM d,yyyy'}" />@H_301_7@

Silverlight 4常用StringFormat格式总结

@H_301_7@

再例如,早期实现一段文字绑定信息需要按照以下格式操作:@H_301_7@

<TextBlock Text=”当前登录用户: ”/>@H_301_7@

<TextBlock Text=“{Binding NamE}”/>@H_301_7@

输出: 当前登录用户Jv9@H_301_7@

在Silverlight 4中使用StringFormat属性,可以简单的实现如下:@H_301_7@

<TextBlock Text=“{Binding Name,StringFormat=’当前登录用户: /{0/}’}”>@H_301_7@

这样实现简单而又方便。@H_301_7@

对于StringFormat属性的使用方法还有很多,下面使用实例演示常用的属性:@H_301_7@

Silverlight 4常用StringFormat格式总结

@H_301_7@@H_301_7@

 @H_301_7@

  1  < Grid  x:Name ="LayoutRoot" >
  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='正在输入 &quot;/{0/}&quot;."  FontWeight ="Bold"  Margin ="0,4,2" />
 10  < TextBlock  Text ="{Binding text, ELEMENTNAME=txtsource, StringFormat='正在输入 &quot;/{0/}&quot;.'}"  textwrapping ="Wrap"  Margin ="0,4" />
 11  < TextBlock  textwrapping ="Wrap"  text ="StringFormat=/{0/,20/}"  FontWeight ="Bold"  Margin ="0,2" />
 12  < TextBlock  Text ="{Binding text, StringFormat=/{0/,20/}}"  textwrapping ="Wrap"  Margin ="0,4"   />
 13  < TextBlock  textwrapping ="Wrap"  text ="StringFormat=/{0/,-20/}"  FontWeight ="Bold"  Margin ="0,2" />
 14  < TextBlock  Text ="{Binding text,-20/}}"  textwrapping ="Wrap"  Margin ="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,2" />
 23  < TextBlock  textwrapping ="Wrap"  text ="{Binding CurrentdatetiR_358_11845@e, StringFormat=f}"  Margin ="8,0"   />
 24  < TextBlock  textwrapping ="Wrap"  text ="StringFormat=F"  FontWeight ="Bold"  Margin ="0,2" />
 25  < TextBlock  textwrapping ="Wrap"  text ="{Binding CurrentdatetiR_358_11845@e, StringFormat=F}"  Margin ="8,0"   />
 26  < TextBlock  textwrapping ="Wrap"  text ="StringFormat=g"  FontWeight ="Bold"  Margin ="0,2" />
 27  < TextBlock  textwrapping ="Wrap"  text ="{Binding CurrentdatetiR_358_11845@e, StringFormat=g}"  Margin ="8,0"   />
 28  < TextBlock  textwrapping ="Wrap"  text ="StringFormat=G"  FontWeight ="Bold"  Margin ="0,2" />
 29  < TextBlock  textwrapping ="Wrap"  text ="{Binding CurrentdatetiR_358_11845@e, StringFormat=G}"  Margin ="8,0"   />
 30  < TextBlock  textwrapping ="Wrap"  text ="StringFormat=m"  FontWeight ="Bold"  Margin ="0,2" />
 31  < TextBlock  textwrapping ="Wrap"  text ="{Binding CurrentdatetiR_358_11845@e, StringFormat=m}"  Margin ="8,0"   />
 32  < TextBlock  textwrapping ="Wrap"  text ="StringFormat=y"  FontWeight ="Bold"  Margin ="0,2" />
 33  < TextBlock  textwrapping ="Wrap"  text ="{Binding CurrentdatetiR_358_11845@e, StringFormat=y}"  Margin ="8,0"   />
 34  < TextBlock  textwrapping ="Wrap"  text ="StringFormat=t"  FontWeight ="Bold"  Margin ="0,2" />
 35  < TextBlock  textwrapping ="Wrap"  text ="{Binding CurrentdatetiR_358_11845@e, StringFormat=t}"  Margin ="8,0"   />
 36  < TextBlock  textwrapping ="Wrap"  text ="StringFormat=T"  FontWeight ="Bold"  Margin ="0,2" />
 37  < TextBlock  textwrapping ="Wrap"  text ="{Binding CurrentdatetiR_358_11845@e, StringFormat=T}"  Margin ="8,0"   />
 38  < TextBlock  textwrapping ="Wrap"  text ="StringFormat=u"  FontWeight ="Bold"  Margin ="0,2" />
 39  < TextBlock  textwrapping ="Wrap"  text ="{Binding CurrentdatetiR_358_11845@e, StringFormat=u}"  Margin ="8,0"   />
 40  < TextBlock  textwrapping ="Wrap"  text ="StringFormat=o"  FontWeight ="Bold"  Margin ="0,2" />
 41  < TextBlock  textwrapping ="Wrap"  text ="{Binding CurrentdatetiR_358_11845@e, StringFormat=o}"  Margin ="8,0"   />
 42  < TextBlock  textwrapping ="Wrap"  text ="StringFormat=MM/dd/yy"  FontWeight ="Bold"  Margin ="0,2" />
 43  < TextBlock  textwrapping ="Wrap"  text ="{Binding CurrentdatetiR_358_11845@e, StringFormat=MM/dd/yy}"  Margin ="8,0" />
 44  < TextBlock  textwrapping ="Wrap"  text ="StringFormat=MMMM dd/, yyyy g"  FontWeight ="Bold"  Margin ="0,2" />
 45  < TextBlock  textwrapping ="Wrap"  text ="{Binding CurrentdatetiR_358_11845@e, StringFormat=MMMM dd/, yyyy g}"  Margin ="8,0"   />
 46  < TextBlock  textwrapping ="Wrap"  text ="StringFormat=hh:mm:ss.fff tt"  FontWeight ="Bold"  Margin ="0,2" />
 47  < TextBlock  textwrapping ="Wrap"  text ="{Binding CurrentdatetiR_358_11845@e, StringFormat=hh:mm:ss.fff tt}"  Margin ="8,0"   />
 48  < TextBlock  textwrapping ="Wrap"  text ="StringFormat=/{0:r/}"  FontWeight ="Bold"  Margin ="0,2" />
 49  < TextBlock  textwrapping ="Wrap"  text ="{Binding CurrentdatetiR_358_11845@e, StringFormat=/{0:r/}}"  Margin ="8,0"   />
 50  < TextBlock  textwrapping ="Wrap"  text ="StringFormat='MMM d, yyyy'"  FontWeight ="Bold"  Margin ="0,2" />
 51  < TextBlock  textwrapping ="Wrap"  text ="{Binding CurrentdatetiR_358_11845@e, StringFormat='MMM d, yyyy'}"  Margin ="8,0"   />
 52  </ StackPanel >
 53  </ Border >  
 54  </ StackPanel >
 55  </ StackPanel >
 56  < StackPanel  Orientation ="Vertical"   >
 57  < TextBlock  Text ="StringFormat和数字演示"  Margin ="8"  FontSize ="16" />
 58  < Border  BorderBrush ="LightGray"  BorderThickness ="1"  Width ="300"  Margin ="5"  HorizontalAlignment ="Left" >
 59  < StackPanel  Orientation ="Vertical"  Margin ="5" >
 60  < Grid  >
 61  < Grid.RowDeFinitions >
 62  < RowDeFinitio Height ="Auto" />
 63  < RowDeFinitio Height ="Auto" />
 64  </ Grid.RowDeFinitions >
 65  < Grid.columnDeFinitions >
 66  < columnDeFinitio Width ="Auto"  MinWidth ="18" />
 67  < columnDeFinition />
 68  < columnDeFinitio Width ="Auto"  MinWidth ="28" />
 69  </ Grid.columnDeFinitions >
 70  < TextBlock  Text ="1"  HorizontalAlignment ="Left"  d:LayoutOverrides ="Height"   />
 71  < TextBlock  Text ="100,000"  d:LayoutOverrides ="Width, Height"  Grid.column ="2"  Margin ="0"   />
 72  < Slider  x:Name ="BigNumSlider"  Margin ="0"  d:LayoutOverrides ="Height"  Grid.columnSpan ="3"  Grid.Row ="1"  Maximum ="100000"  Minimum ="1"  Value ="50000" />
 73  </ Grid >
 74  < TextBlock  textwrapping ="Wrap"  text ="StringFormat=c"  FontWeight ="Bold"  Margin ="0,2" />
 75  < TextBox  textwrapping ="Wrap"  text ="{Binding Value, ELEMENTNAME=BigNumSlider, Mode=TwoWay, StringFormat=c}"  Margin ="8,0" />
 76  < TextBlock  textwrapping ="Wrap"  text ="StringFormat=目前拥有现金 /{0:C0/}"  FontWeight ="Bold"  Margin ="0,2" />
 77  < TextBox  textwrapping ="Wrap"  text ="{Binding Value, StringFormat=目前拥有现金 /{0:C0/}}"  Margin ="8,0" />
 78  < TextBlock  textwrapping ="Wrap"  text ="StringFormat=e"  FontWeight ="Bold"  Margin ="0,2" />
 79  < TextBox  textwrapping ="Wrap"  text ="{Binding Value, StringFormat=E}"  Margin ="8,0" />
 80  < TextBlock  textwrapping ="Wrap"  text ="StringFormat=/{0:E4/}"  FontWeight ="Bold"  Margin ="0,2" />
 81  < TextBox  textwrapping ="Wrap"  text ="{Binding Value, StringFormat=/{0:E4/}}"  Margin ="8,0" />
 82  < TextBlock  textwrapping ="Wrap"  text ="StringFormat=n"  FontWeight ="Bold"  Margin ="0,2" />
 83  < TextBox  textwrapping ="Wrap"  text ="{Binding Value, StringFormat=n}"  Margin ="8,0" />
 84  < TextBlock  textwrapping ="Wrap"  text ="StringFormat=/{0:N0/}"  FontWeight ="Bold"  Margin ="0,2" />
 85  < TextBox  textwrapping ="Wrap"  text ="{Binding Value, StringFormat=/{0:N0/}}"  Margin ="8,0" />
 86  < TextBlock  textwrapping ="Wrap"  text ="StringFormat='###,###.##'"  FontWeight ="Bold"  Margin ="0,2" />
 87  < TextBox  textwrapping ="Wrap"  text ="{Binding Value, StringFormat='###,###.##'}"  Margin ="8,0"   />
 88  </ StackPanel >
 89  </ Border >
 90  < Border  BorderBrush ="LightGray"  BorderThickness ="1"  Width ="300"  Margin ="5"  HorizontalAlignment ="Left" >
 91  < StackPanel  Orientation ="Vertical"  Margin ="5" >
 92  < Grid  >
 93  < Grid.RowDeFinitions >
 94  < RowDeFinitio Height ="Auto" />
 95  < RowDeFinitio Height ="Auto" />
 96  </ Grid.RowDeFinitions >
 97  < Grid.columnDeFinitions >
 98  < columnDeFinitio Width ="Auto"  MinWidth ="18" />
 99  < columnDeFinition />
100  < columnDeFinitio Width ="Auto"  MinWidth ="28" />
101  </ Grid.columnDeFinitions >
102  < TextBlock  Text ="-1"  HorizontalAlignment ="Left"  d:LayoutOverrides ="Height"   />
103  < TextBlock  Text ="1"  d:LayoutOverrides ="Width, Height"  Grid.column ="2"  Margin ="0"   />
104  < Slider  x:Name ="BigNumSlider1"  Margin ="0"  d:LayoutOverrides ="Height"  Grid.columnSpan ="3"  Grid.Row ="1"  Maximum ="1"  Minimum ="-1"  Value ="0.2"  LargeChange ="0.1"  smallChange ="0.01" />
105  </ Grid >
106  < TextBlock  textwrapping ="Wrap"  text ="StringFormat=n"  FontWeight ="Bold"  Margin ="0,2" />
107  < TextBox  textwrapping ="Wrap"  text ="{Binding Value, ELEMENTNAME=BigNumSlider1,0" />
108  < TextBlock  textwrapping ="Wrap"  text ="StringFormat=/{0:N4/}"  FontWeight ="Bold"  Margin ="0,2" />
109  < TextBox  textwrapping ="Wrap"  text ="{Binding Value, StringFormat=/{0:N4/}}"  Margin ="8,0" />
110  < TextBlock  textwrapping ="Wrap"  text ="StringFormat=p"  FontWeight ="Bold"  Margin ="0,2" />
111  < TextBox  textwrapping ="Wrap"  text ="{Binding Value, StringFormat=p}"  Margin ="8,0" />
112  < TextBlock  textwrapping ="Wrap"  text ="StringFormat=/{0:P0/}"  FontWeight ="Bold"  Margin ="0,2" />
113  < TextBox  textwrapping ="Wrap"  text ="{Binding Value, StringFormat=/{0:P0/}}"  Margin ="8,0" />
114  < TextBlock  textwrapping ="Wrap"  text ="StringFormat=/{0:E5/}"  FontWeight ="Bold"  Margin ="0,2" />
115  < TextBox  textwrapping ="Wrap"  text ="{Binding Value, StringFormat=/{0:E5/}}"  Margin ="8,0" />
116  < TextBlock  textwrapping ="Wrap"  text ="StringFormat=/{0:C3/}"  FontWeight ="Bold"  Margin ="0,2" />
117  < TextBox  textwrapping ="Wrap"  text ="{Binding Value, StringFormat=/{0:C3/}}"  Margin ="8,0" />
118  </ StackPanel >
119  </ Border >
120  </ StackPanel >
121 
122  </ StackPanel >
123  </ ScrollViewer >
124  </ Grid >

 @H_301_7@

 @H_301_7@

源代码下载@H_301_7@

在线演示@H_301_7@

 @H_301_7@

欢迎大家加入“专注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人@H_301_7@

大佬总结

以上是大佬教程为你收集整理的Silverlight 4常用StringFormat格式总结全部内容,希望文章能够帮你解决Silverlight 4常用StringFormat格式总结所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。