詳解Silverlight 4中的數(shù)據(jù)綁定
本文將為大家介紹Silverlight 4中的數(shù)據(jù)綁定,希望能對(duì)大家有所幫助。同時(shí)51CTO向您推薦《走向銀光 —— 一步一步學(xué)Silverlight》專題。
DependencyObject Binding
在Silverlight之前的版本中,其支持的元素綁定只是允許綁定繼承自FrameworkElement類下元素,但是比如一些形變比如Transformations就不能綁定了?,F(xiàn)在數(shù)據(jù)綁定也可以綁定繼承自DependencyObject下的任何元素。
- <Grid x:Name="LayoutRoot"
- Background="White">
- <StackPanel Width="400" Margin="0,22,0,0">
- <StackPanel.RenderTransform>
- <CompositeTransform
- ScaleX="{Binding Value,ElementName=stretcher}"
- ScaleY="{Binding Value,ElementName=stretcher}" />
- </StackPanel.RenderTransform>
- <Button Content="Button"/>
- <Button Content="Button"/>
- <Button Content="Button"/>
- <Button Content="Button"/>
- <Button Content="Button"/>
- </StackPanel>
- <Slider Minimum=".5"
- Maximum="4"
- x:Name="stretcher"
- Value="1" VerticalAlignment="Top" />
- </Grid>
String Formatting
新版的Silverlight4中新增加了格式化字符串的能力。在這之前如果要做一個(gè)數(shù)據(jù)格式化不得不使用一個(gè)Converter來格式化字符串。現(xiàn)在可以使用擴(kuò)展標(biāo)記StringFormat來做一些比如日期、貨幣等的格式化。
在VS2010中也提供了可視化的支持。
- <Grid x:Name="LayoutRoot" Background="White">
- <TextBox Text="{Binding ReleaseDate, StringFormat='yyyy年MM月dd日',
- Mode=TwoWay}"
- Margin="0,30,0,0"
- Height="26"
- VerticalAlignment="Top" d:LayoutOverrides="Height" />
- <TextBlock Text="{Binding Price, StringFormat='c'}"
- Margin="0,0,0,0"
- Height="26" VerticalAlignment="Top" />
- </Grid>
Null and Fallback Values
在某些特殊的情況下,數(shù)據(jù)有可能加載失敗。數(shù)據(jù)綁定中有新增加了兩個(gè)寬展標(biāo)記TargetNullValue、FallbackValue,TargetNullValue這個(gè)標(biāo)記表示了當(dāng)綁定值是null的時(shí)候顯示的值。FallbackValue則是在數(shù)據(jù)未綁定時(shí)顯示的值。
- <Grid x:Name="LayoutRoot" Background="White">
- <TextBlock Text="{Binding Developer,
- TargetNullValue='(暫無)'}"
- Height="26" Margin="0,100,0,0"
- VerticalAlignment="Top" d:LayoutOverrides="Height" />
- <TextBlock Text="{Binding Publisher,
- FallbackValue='(暫無)'}" Height="26"
- VerticalAlignment="Top" Margin="0,33,0,0" />
- </Grid>
- <UserControl.Resources>
- <CollectionViewSource x:Name="dataSource"
- Source="{Binding}">
- <CollectionViewSource.GroupDescriptions>
- <PropertyGroupDescription PropertyName="Gender" />
- <PropertyGroupDescription PropertyName="AgeGroup" />
- </CollectionViewSource.GroupDescriptions>
- <CollectionViewSource.SortDescriptions>
- <compMod:SortDescription PropertyName="AgeGroup" Direction="Ascending"/>
- </CollectionViewSource.SortDescriptions>
- </CollectionViewSource>
- </UserControl.Resources>
- <Grid x:Name="LayoutRoot" Background="White">
- <sdk:DataGrid ItemsSource="{Binding Source={StaticResource dataSource}}" />
- </Grid>
- public List<Person> GetPeople()
- { List<Person> peeps = new List<Person>();
- peeps.Add(new Person() { FirstName = "Wang", LastName = "Zhe", Gender = "M", AgeGroup = "Adult" });
- peeps.Add(new Person() { FirstName = "nasa", LastName = "wang", Gender = "M", AgeGroup = "Adult" });
- peeps.Add(new Person() { FirstName = "summer", LastName = "liang", Gender = "F", AgeGroup = "Kid" });
- peeps.Add(new Person() { FirstName = "liang", LastName = "jing", Gender = "F", AgeGroup = "Kid" });
- return peeps;
- }
Error Propogation
Silverlight的數(shù)據(jù)驗(yàn)證機(jī)制,在這里得到了很多的擴(kuò)充,提供了IDataErrorInfo、INotifyDataErrorInfo從而能得到更多的信息。
原文標(biāo)題:Silverlight 4 中數(shù)據(jù)綁定發(fā)生的變化
鏈接:http://www.cnblogs.com/nasa/archive/2010/04/19/Data_Binding_Changes_in_Silverlight_4.html






