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

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>
=================================


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

 

=================================
    <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的数据验证机制,在这里得到了很多的扩充,提供了IDataErrorInfoINotifyDataErrorInfo从而能得到更多的信息。

 


posted @ 2010-04-19 17:45  王喆(nasa)  阅读(1931)  评论(5编辑  收藏  举报