詳解Silverlight 4中的數據綁定
本文將為大家介紹Silverlight 4中的數據綁定,希望能對大家有所幫助。同時51CTO向您推薦《走向銀光 —— 一步一步學Silverlight》專題。
DependencyObject Binding
在Silverlight之前的版本中,其支持的元素綁定只是允許綁定繼承自FrameworkElement類下元素,但是比如一些形變比如Transformations就不能綁定了。現在數據綁定也可以綁定繼承自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中新增加了格式化字符串的能力。在這之前如果要做一個數據格式化不得不使用一個Converter來格式化字符串。現在可以使用擴展標記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
在某些特殊的情況下,數據有可能加載失敗。數據綁定中有新增加了兩個寬展標記TargetNullValue、FallbackValue,TargetNullValue這個標記表示了當綁定值是null的時候顯示的值。FallbackValue則是在數據未綁定時顯示的值。
- <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的數據驗證機制,在這里得到了很多的擴充,提供了IDataErrorInfo、INotifyDataErrorInfo從而能得到更多的信息。
原文標題:Silverlight 4 中數據綁定發生的變化
鏈接:http://www.cnblogs.com/nasa/archive/2010/04/19/Data_Binding_Changes_in_Silverlight_4.html


2009-11-26 13:12:16
2009-12-31 11:10:01
2009-11-18 11:33:23




