Silverlight 2系列(11):数据绑定

[ 3202 查看 / 0 回复 ]

概念<ŠP”/*Ëi£°nfhot.comvO{' ÐK
Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, Ironpython,对JSON、Web Service、WCF以及Sockets的支持等一系列新的特性。《一步一步学Silverlight 2系列》文章带您快速进入Silverlight 2开发。<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
本文为系列文章第十一篇,主要介绍Silverlight 2中的数据绑定。<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
数据绑定模式<ŠP”/*Ëi£°nfhot.comvO{' ÐK
在Silverlight 2中,支持三种模式的数据绑定。<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
1.OneTime:一次绑定,在绑定创建时使用源数据更新目标,适用于只显示数据而不进行数据的更新。<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
2.OneWay:单向绑定,在绑定创建时或者源数据发生变化时更新到目标,适用于显示变化的数据。<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
3.TwoWay:双向绑定,在任何时候都可以同时更新源数据和目标。<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
Jesse Liberty举的例子非常的形象,使用Silverlight开发一个在线书店,显示书籍的书名、作者等信息,使用OneTime模式,这些数据一般不会发生变化的;显示价格信息时使用OneWay模式,因为管理员可能会在一天内调整价格;显示书籍的剩余数量时用TwoWay模式,数量随着用户的订购会随时发生变化,即目标和源数据都要进行更新。<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
简单数据绑定<ŠP”/*Ëi£°nfhot.comvO{' ÐK
在本示例中我们将做一个简单的数据绑定,用来显示用户信息,XAML如下:
  1. <Grid x:Name="LayoutRoot" Background="#46461F">
  2. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  3.     <Grid.RowDefinitions>
  4. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  5.         <RowDefinition Height="160"></RowDefinition>
  6. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  7.         <RowDefinition Height="40"></RowDefinition>
  8. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  9.         <RowDefinition Height="40"></RowDefinition>
  10. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  11.     </Grid.RowDefinitions>
  12. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  13.     <Grid.ColumnDefinitions>
  14. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  15.         <ColumnDefinition Width="150"></ColumnDefinition>
  16. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  17.         <ColumnDefinition Width="*"></ColumnDefinition>
  18. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  19.     </Grid.ColumnDefinitions>
  20. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  21.     <Image Source="terrylee.jpg" Width="78" Height="100"
  22. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  23.           HorizontalAlignment="Left" Grid.Row="0" Grid.Column="1"/>
  24. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  25.     <TextBlock Foreground="White" FontSize="18" Text="姓名:"
  26. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  27.               Grid.Row="1" Grid.Column="0" HorizontalAlignment="Right"/>
  28. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  29.     <TextBlock x:Name="lblName" Foreground="White" FontSize="18"
  30. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  31.               Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left"/>
  32. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  33.     <TextBlock Foreground="White" FontSize="18" Text="位置:"
  34. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  35.               Grid.Row="2" Grid.Column="0" HorizontalAlignment="Right"/>
  36. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  37.     <TextBlock x:Name="lblAddress" Foreground="White" FontSize="18"
  38. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  39.               Grid.Row="2" Grid.Column="1" HorizontalAlignment="Left"/>
  40. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  41. </Grid>
  42. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
复制代码
添加一个简单User类,它具有Name和Address两个属性:
  1. public class User
  2. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  3. {
  4. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  5.     public string Name { get; set; }
  6. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK

  7. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  8.     public string Address { get; set; }
  9. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  10. }
  11. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
复制代码
使用绑定句法{Binding Property}进行数据绑定,注意下面的两个TextBlock控件Text属性:
  1. <Grid x:Name="LayoutRoot" Background="#46461F">
  2. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  3.     <Grid.RowDefinitions>
  4. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  5.         <RowDefinition Height="160"></RowDefinition>
  6. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  7.         <RowDefinition Height="40"></RowDefinition>
  8. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  9.         <RowDefinition Height="40"></RowDefinition>
  10. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  11.     </Grid.RowDefinitions>
  12. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  13.     <Grid.ColumnDefinitions>
  14. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  15.         <ColumnDefinition Width="150"></ColumnDefinition>
  16. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  17.         <ColumnDefinition Width="*"></ColumnDefinition>
  18. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  19.     </Grid.ColumnDefinitions>
  20. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  21.     <Image Source="terrylee.jpg" Width="78" Height="100"
  22. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  23.       HorizontalAlignment="Left" Grid.Row="0" Grid.Column="1"/>
  24. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  25.     <TextBlock Foreground="White" FontSize="18" Text="姓名:"
  26. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  27.           Grid.Row="1" Grid.Column="0" HorizontalAlignment="Right"/>
  28. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  29.     <TextBlock x:Name="lblName" Foreground="White" FontSize="18"
  30. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  31.               Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left"
  32. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  33.               Text="{Binding Name}"/>
  34. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  35.     <TextBlock Foreground="White" FontSize="18" Text="位置:"
  36. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  37.               Grid.Row="2" Grid.Column="0" HorizontalAlignment="Right"/>
  38. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  39.     <TextBlock x:Name="lblAddress" Foreground="White" FontSize="18"
  40. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  41.               Grid.Row="2" Grid.Column="1" HorizontalAlignment="Left"
  42. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  43.               Text="{Binding Address}"/>
  44. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  45. </Grid>
  46. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
复制代码
指定数据源,注意这里是创建一个User的实例并赋值后,把user实例绑定到了TextBlock的DataContext上,而不是向之前我们所做的示例中那样,直接指定Text属性:
  1. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  2. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  3. {
  4. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  5.     User user = new User();
  6. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  7.     user.Name = "TerryLee";
  8. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  9.     user.Address = "中国 天津";
  10. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK

  11. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  12.     lblName.DataContext = user;
  13. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  14.     lblAddress.DataContext = user;
  15. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  16. }
  17. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
复制代码
运行示例后,可以看到:<ŠP”/*Ëi£°nfhot.comvO{' ÐK

 附件: 您所在的用户组无法下载或查看附件<ŠP”/*Ëi£°nfhot.comvO{' ÐK
上面这种数据绑定模式,只是显示数据而不对数据做任何修改,默认的绑定模式是一次绑定OneTime。<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
单向绑定示例<ŠP”/*Ëi£°nfhot.comvO{' ÐK
如果需要在数据源发生变化时能够通知UI进行相应的更新,即使用单向绑定OneWay或者双向绑定TwoWay,则业务实体需要实现接口INotifyPropertyChanged。在本示例中,我们加上一个更新按钮,当单击按钮时更新user实例的属性值,会看到界面上的数据也会发生变化。<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
修改一下User类,使其实现INotifyPropertyChanged接口。
  1. public class User : INotifyPropertyChanged
  2. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  3. {
  4. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  5.     public event PropertyChangedEventHandler PropertyChanged;
  6. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK

  7. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  8.     private string _name;
  9. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  10.     public string Name
  11. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  12.     {
  13. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  14.         get { return _name; }
  15. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  16.         set
  17. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  18.         {
  19. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  20.             _name = value;
  21. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  22.             if(PropertyChanged != null)
  23. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  24.             {
  25. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  26.                 PropertyChanged(this, new PropertyChangedEventArgs("Name"));
  27. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  28.             }
  29. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  30.         }
  31. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  32.     }
  33. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK

  34. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  35.     private string _address;
  36. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  37.     public string Address
  38. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  39.     {
  40. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  41.         get { return _address; }
  42. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  43.         set
  44. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  45.         {
  46. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  47.             _address = value;
  48. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  49.             if (PropertyChanged != null)
  50. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  51.             {
  52. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  53.                 PropertyChanged(this, new PropertyChangedEventArgs("Address"));
  54. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  55.             }
  56. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  57.         }
  58. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  59.     }
  60. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  61. }
  62. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
复制代码
修改数据绑定模式,使用单向绑定OneWay模式,如{Binding Address, Mode=OneWay}
  1. <Grid x:Name="LayoutRoot" Background="#46461F">
  2. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  3.     <Grid.RowDefinitions>
  4. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  5.         <RowDefinition Height="160"></RowDefinition>
  6. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  7.         <RowDefinition Height="40"></RowDefinition>
  8. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  9.         <RowDefinition Height="40"></RowDefinition>
  10. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  11.     </Grid.RowDefinitions>
  12. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  13.     <Grid.ColumnDefinitions>
  14. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  15.         <ColumnDefinition Width="150"></ColumnDefinition>
  16. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  17.         <ColumnDefinition Width="*"></ColumnDefinition>
  18. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  19.     </Grid.ColumnDefinitions>
  20. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  21.     <Image Source="terrylee.jpg" Width="78" Height="100"
  22. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  23.       HorizontalAlignment="Left" Grid.Row="0" Grid.Column="1"/>
  24. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  25.     <Button x:Name="btnUpdate" Width="100" Height="40"
  26. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  27.             Content="Update" Click="btnUpdate_Click"/>
  28. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  29.     <TextBlock Foreground="White" FontSize="18" Text="姓名:"
  30. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  31.           Grid.Row="1" Grid.Column="0" HorizontalAlignment="Right"/>
  32. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  33.     <TextBlock x:Name="lblName" Foreground="White" FontSize="18"
  34. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  35.               Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left"
  36. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  37.               Text="{Binding Name, Mode=OneWay}"/>
  38. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  39.     <TextBlock Foreground="White" FontSize="18" Text="位置:"
  40. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  41.               Grid.Row="2" Grid.Column="0" HorizontalAlignment="Right"/>
  42. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  43.     <TextBlock x:Name="lblAddress" Foreground="White" FontSize="18"
  44. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  45.               Grid.Row="2" Grid.Column="1" HorizontalAlignment="Left"
  46. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  47.               Text="{Binding Address, Mode=OneWay}"/>
  48. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  49. </Grid>
  50. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
复制代码
编写事件处理程序,为了演示把user声明为一个全局的,并在按钮的单击事件中修改其属性值:
  1. public partial class Page : UserControl
  2. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  3. {
  4. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  5.     public Page()
  6. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  7.     {
  8. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  9.         InitializeComponent();
  10. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  11.     }
  12. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  13.     User user;
  14. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  15.     private void UserControl_Loaded(object sender, RoutedEventArgs e)
  16. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  17.     {
  18. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  19.         user = new User();
  20. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  21.         user.Name = "TerryLee";
  22. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  23.         user.Address = "中国 天津";
  24. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK

  25. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  26.         lblName.DataContext = user;
  27. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  28.         lblAddress.DataContext = user;
  29. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  30.     }
  31. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK

  32. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  33.     private void btnUpdate_Click(object sender, RoutedEventArgs e)
  34. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  35.     {
  36. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  37.         user.Name = "李会军";
  38. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  39.         user.Address = "China Tianjin";
  40. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  41.     }
  42. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  43. }
  44. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
复制代码
运行后如下所示:<ŠP”/*Ëi£°nfhot.comvO{' ÐK

 附件: 您所在的用户组无法下载或查看附件<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
单击Update按钮后:<ŠP”/*Ëi£°nfhot.comvO{' ÐK

 附件: 您所在的用户组无法下载或查看附件<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
绑定到列表<ŠP”/*Ëi£°nfhot.comvO{' ÐK
下面再看一个绑定到列表的简单例子,一般都会使用DataGrid或者ListBox来进行列表数据的显示。下面的示例我们显示一个文章列表:
  1. <Grid Background="#46461F">
  2. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  3.     <Grid.RowDefinitions>
  4. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  5.         <RowDefinition Height="40"></RowDefinition>
  6. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  7.         <RowDefinition Height="*"></RowDefinition>
  8. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  9.     </Grid.RowDefinitions>
  10. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  11.     <Grid.ColumnDefinitions>
  12. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  13.         <ColumnDefinition></ColumnDefinition>
  14. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  15.     </Grid.ColumnDefinitions>
  16. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  17.     <Border Grid.Row="0" Grid.Column="0" CornerRadius="15"
  18. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  19.             Width="240" Height="36" Background="Orange"
  20. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  21.             Margin="20 0 0 0" HorizontalAlignment="Left">
  22. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  23.         <TextBlock Text="文章列表" Foreground="White"
  24. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  25.                   HorizontalAlignment="Left" VerticalAlignment="Center"
  26. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  27.                   Margin="20 0 0 0"></TextBlock>
  28. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  29.     </Border>
  30. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  31.     <ListBox x:Name="PostList" Grid.Column="0" Grid.Row="1"
  32. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  33.             Margin="40 10 10 10"
  34. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  35.             HorizontalContentAlignment="Left" VerticalContentAlignment="Bottom"
  36. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  37.             ItemsSource="{Binding Posts}">
  38. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  39.     </ListBox>
  40. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  41. </Grid>
  42. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
复制代码
编写一个简单的业务类:
  1. public class Blog
  2. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  3. {
  4. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  5.     public List<String> Posts { get; set; }
  6. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  7. }
  8. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
复制代码
初始化集合数据并进行绑定
  1. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  2. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  3. {
  4. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  5.     Blog blog = new Blog();
  6. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  7.     blog.Posts = new List<String>
  8. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  9.     {
  10. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  11.         "一步一步学Silverlight 2系列(10):使用用户控件",
  12. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  13.         "一步一步学Silverlight 2系列(9):使用控件模板",
  14. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  15.         "一步一步学Silverlight 2系列(8):使用样式封装控件观感",
  16. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  17.         "一步一步学Silverlight 2系列(7):全屏模式支持"
  18. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  19.     };
  20. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK

  21. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  22.     PostList.DataContext = blog;
  23. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
  24. }
  25. <ŠP”/*Ëi£°nfhot.comvO{' ÐK<ŠP”/*Ëi£°nfhot.comvO{' ÐK <ŠP”/*Ëi£°nfhot.comvO{' ÐK
复制代码
最终运行的结果如下所示:<ŠP”/*Ëi£°nfhot.comvO{' ÐK

 附件: 您所在的用户组无法下载或查看附件<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
当然我们也可以使用ListBox的ItemsSource属性进行绑定,<ŠP”/*Ëi£°nfhot.comvO{' ÐK
<ŠP”/*Ëi£°nfhot.comvO{' ÐK
结束语<ŠP”/*Ëi£°nfhot.comvO{' ÐK
本文简单介绍了Silverlight 2中的数据绑定,你可以从这里下载文章示例代码。<ŠP”/*Ëi£°nfhot.comvO{' ÐK
学而时习之,不亦说乎?有朋自远方来,不亦乐乎?人不知而不愠,不亦君子乎?
知之为知之,不知为不知,是知也。
TOP