热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> Silverlight>>正文内容

Silverlight 4 中数据绑定发生的变化

发布时间:2010年04月21日点击数: 佚名

DependencyObject Binding            【源码下载

在Silverlight之前的版本中,其支持的元素绑定只是允许绑定继承自FrameworkElement类下元素,但是比如一些形变比如Transformations就不能绑定了。现在数据绑定也可以绑定继承自DependencyObject下的任何元素。

  1. <Grid x:Name="LayoutRoot" 
  2. Background="White"> 
  3.   <StackPanel Width="400" Margin="0,22,0,0"> 
  4.       <StackPanel.RenderTransform> 
  5.           <CompositeTransform 
  6.   ScaleX="{Binding Value,ElementName=stretcher}" 
  7.   ScaleY="{Binding Value,ElementName=stretcher}" /> 
  8.       </StackPanel.RenderTransform> 
  9.    <Button Content="Button"/> 
  10.    <Button Content="Button"/> 
  11.    <Button Content="Button"/> 
  12.    <Button Content="Button"/> 
  13.    <Button Content="Button"/> 
  14.   </StackPanel> 
  15.   <Slider Minimum=".5" 
  16.     Maximum="4" 
  17.     x:Name="stretcher" 
  18.     Value="1" VerticalAlignment="Top" /> 
  19. </Grid> 

String Formatting
新版的Silverlight4中新增加了格式化字符串的能力。在这之前如果要做一个数据格式化不得不使用一个Converter来格式化字符串。现在可以使用扩展标记StringFormat来做一些比如日期、货币等的格式化。

在VS2010中也提供了可视化的支持。

  1. <Grid x:Name="LayoutRoot" Background="White"> 
  2. <TextBox Text="{Binding ReleaseDate, StringFormat='yyyy年MM月dd日'
  3.                 Mode=TwoWay}" 
  4.          Margin="0,30,0,0" 
  5.          Height="26" 
  6.          VerticalAlignment="Top" d:LayoutOverrides="Height" /> 
  7. <TextBlock Text="{Binding Price, StringFormat='c'}" 
  8.            Margin="0,0,0,0" 
  9.            Height="26" VerticalAlignment="Top" /> 
  10. </Grid>

Null and Fallback Values
在某些特殊的情况下,数据有可能加载失败。数据绑定中有新增加了两个宽展标记TargetNullValue、FallbackValue,TargetNullValue这个标记表示了当绑定值是null的时候显示的值。FallbackValue则是在数据未绑定时显示的值。

  1. <Grid x:Name="LayoutRoot" Background="White"> 
  2. <TextBlock Text="{Binding Developer, 
  3.     TargetNullValue='(暂无)'}" 
  4.            Height="26" Margin="0,100,0,0" 
  5.            VerticalAlignment="Top" d:LayoutOverrides="Height" /> 
  6. <TextBlock Text="{Binding Publisher, 
  7.     FallbackValue='(暂无)'}" Height="26" 
  8.            VerticalAlignment="Top" Margin="0,33,0,0" /> 
  9. </Grid> 

CollectionViewSource Changes
对于在GataGrid中做分组管理,现在的CollectionViewSource支持数据到GroupDescriptions的绑定,这样可以更加轻松的在XAML做分组。

  1. <UserControl.Resources> 
  2.   <CollectionViewSource x:Name="dataSource" 
  3. Source="{Binding}"> 
  4.       <CollectionViewSource.GroupDescriptions> 
  5.           <PropertyGroupDescription PropertyName="Gender" /> 
  6.           <PropertyGroupDescription PropertyName="AgeGroup" /> 
  7.       </CollectionViewSource.GroupDescriptions> 
  8.       <CollectionViewSource.SortDescriptions> 
  9.           <compMod:SortDescription PropertyName="AgeGroup" Direction="Ascending"/>                
  10.       </CollectionViewSource.SortDescriptions> 
  11.   </CollectionViewSource> 
  12. </UserControl.Resources> 
  13. <Grid x:Name="LayoutRoot" Background="White"> 
  14.     <sdk:DataGrid ItemsSource="{Binding Source={StaticResource dataSource}}" /> 
  15. </Grid> 
  1. public List<Person> GetPeople() 
  2.        { 
  3.            List<Person> peeps = new List<Person>(); 
  4.            peeps.Add(new Person() { FirstName = "Wang", LastName = "Zhe", Gender = "M", AgeGroup = "Adult" }); 
  5.            peeps.Add(new Person() { FirstName = "nasa", LastName = "wang", Gender = "M", AgeGroup = "Adult" }); 
  6.            peeps.Add(new Person() { FirstName = "summer", LastName = "liang", Gender = "F", AgeGroup = "Kid" }); 
  7.            peeps.Add(new Person() { FirstName = "liang", LastName = "jing", Gender = "F", AgeGroup = "Kid" }); 
  8.            return peeps; 
  9.        } 

Error Propogation
Silverlight的数据验证机制,在这里得到了很多的扩充,提供了IDataErrorInfo、INotifyDataErrorInfo从而能得到更多的信息。

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作