ZOL首页产品报价论坛软件下载问答堂>>中关村在线手机版更多

今日导读
热点推荐
ZOL首页 > 商业软件 > 微软产品专区 > 开发软件 > 详解Silverlight 4中的数据绑定

详解Silverlight 4中的数据绑定


【转载】 CBSi中国·ZOL 10年04月26日 [评论]



  本文将为大家介绍Silverlight 4中的数据绑定,希望能对大家有所帮助。

  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=""   
  7.   ScaleY="" />    
  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=""    
  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="">    
  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="}" />    
  15.  </Grid>  

 

  1. public List<Person> GetPeople()          
  2. {            List<Person> peeps = new List<Person>();  
  3.  peeps.Add(new Person() { FirstName = "Wang", LastName = "Zhe", Gender = "M", AgeGroup = "Adult" });  
  4.  peeps.Add(new Person() { FirstName = "nasa", LastName = "wang", Gender = "M", AgeGroup = "Adult" });  
  5.  peeps.Add(new Person() { FirstName = "summer", LastName = "liang", Gender = "F", AgeGroup = "Kid" });  
  6.  peeps.Add(new Person() { FirstName = "liang", LastName = "jing", Gender = "F", AgeGroup = "Kid" });  
  7.     return peeps;  
  8.         } 

  Error Propogation

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

 

 

软件论坛热门帖子
软件资讯

软件频道

软件教程
技巧应用
软件论坛