详解Silverlight 4中的数据绑定

开发 前端 后端
本文将为大家讲述的是Silverlight 4中的数据绑定的有关内容,希望能对大家有所帮助。

本文将为大家介绍Silverlight 4中的数据绑定,希望能对大家有所帮助。同时51CTO向您推荐《走向银光 —— 一步一步学Silverlight》专题。

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. {            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从而能得到更多的信息。

原文标题:Silverlight 4 中数据绑定发生的变化

链接:http://www.cnblogs.com/nasa/archive/2010/04/19/Data_Binding_Changes_in_Silverlight_4.html

 

责任编辑:彭凡 来源: 博客园
相关推荐

2009-12-30 09:38:37

Silverlight

2009-02-20 08:54:20

DownloaderSilverlight对象

2009-12-30 09:45:52

Silverlight

2009-11-26 13:12:16

Silverlight

2010-06-02 09:25:29

Silverlight

2009-12-31 11:10:01

2016-10-11 20:33:17

JavaScriptThisWeb

2009-03-16 10:00:01

文件同步LivemeshSilverlight

2009-12-30 10:15:57

Silverlight

2012-05-28 10:34:50

MVVM 数据绑定

2009-11-18 11:33:23

Silverlight

2009-12-30 09:55:51

Silverlight

2009-12-30 15:32:03

Silverlight

2009-09-07 15:25:24

MySQL数据库互操作Silverlight

2023-10-07 11:04:58

WPF数据UI

2009-12-31 16:56:40

Silverlight

2009-12-31 10:07:08

Silverlight

2010-07-30 10:45:08

Flex数据绑定

2009-07-27 09:46:28

Silverlight

2009-12-30 14:36:29

Silverlight
点赞
收藏

51CTO技术栈公众号