热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> Silverlight>>正文内容

Silverlight中的数据绑定4

发布时间:2010年08月13日点击数: 佚名

在前面的几篇文章中,关于Silverlight中数据方面主要是以展现一个Person类的数组为例,其实数据源也可以是集合类型的。

       在Silverlight中创建数据源集合,可以使用内建的ObservableCollection类,为了实现动态绑定,以使集合中的插入或移除操作可以自动更新 UI,则数据源集合需要实现INotifyPropertyChanged接口,INotifyCollectionChanged接口,而ObservableCollection实现了这2个接口外,还实现了Add,Remove,Clear,Insert操作。

      下面仍然在前面的示例基础上进行修改,实现绑定到集合
首先仍然用之前的Person类,

  1. public class Person:INotifyPropertyChanged 
  2.     { 
  3.         public event PropertyChangedEventHandler PropertyChanged; 
  4.         public Person() { } 
  5.         private string firstName; 
  6.         public string FirstName 
  7.         { 
  8.             get { return firstName; } 
  9.             set { 
  10.                 if (value.Length>6) 
  11.                 { 
  12.                     throw new Exception("数据太长"); 
  13.                 } 
  14.                 firstName = value; 
  15.             if (PropertyChanged != null
  16.             { 
  17.                 PropertyChanged(thisnew PropertyChangedEventArgs("FirstName")); 
  18.             } 
  19.             } 
  20.         } 
  21.         private string lastName; 
  22.         public string LastName 
  23.         { 
  24.             get { return lastName; } 
  25.             set { 
  26.                 if (String.IsNullOrEmpty(value)) 
  27.                 { 
  28.                     throw new ArgumentException(); 
  29.                 } 
  30.                 lastName = value; 
  31.                 if (PropertyChanged != null
  32.                 { 
  33.                     PropertyChanged(thisnew PropertyChangedEventArgs("LastName")); 
  34.                 } 
  35.             } 
  36.         } 
  37.         public void NotifyPropertyChanged(string propertyname) 
  38.         { 
  39.             if (PropertyChanged != null
  40.             { 
  41.                 PropertyChanged(this,new PropertyChangedEventArgs(propertyname)); 
  42.             } 
  43.         } 
  44.          } 

接着创建集合类Persons.cs

  1. public class Persons:ObservableCollection<Person> 
  2.    

不要忘记添加using System.Collections.ObjectModel;
      接下来修改一下XAML,仍然只贴出关键部分

  1. <Grid x:Name="LayoutRoot" Background="White"> 
  2.    <ListBox Height="100"   ItemsSource="{Binding Mode=OneWay}" /> 
  3. </Grid> 

后台代码只要在原来的基础上变换一下

  1.  InitializeComponent(); 
  2. allPersons = new Persons(); 
  3. allPersons.Add(new Person{FirstName="david",LastName="Grenn"}); 
  4. allPersons.Add(new Person{FirstName="dam",LastName="white"}); 
  5. allPersons.Add(new Person { FirstName = "tom", LastName = "Smith" }); 
  6. this.LayoutRoot.DataContext = allPersons; 

这里注意一下概念,DataContext是可继承的,如果设置了父节点的DataContext属性,那么所有子节点拥有相同的DataContext
运行,效果如下:

QQ截图未命名

ListBox中显示的数据并不是我们期望得到的,而是为"DataBindSilverlight.Person”的字符串 ,根本原因在于,在本例中传递给ListBox项的是一 个Person对象,因为在没有为ListBox控件指定数据模板的情况下,SL会使用默认的ToString()方法,将Person对象转换成字符串显示,因此会出现这样的情况。那么要改变这个状况,就需要为ListBox定制数据模板控制Person对象的呈现。

数据模板控制呈现

        数据绑定可以将业务逻辑与用户界面元素很方便的关联,但是并不是说可以完全展现表示业务对象的信息,这时候就需要考虑数据模板来解决上面出现的问题,只要将XAML代码修改如下

  1. <UserControl.Resources> 
  2.         <DataTemplate x:Key="template"> 
  3.             <Grid> 
  4.                <TextBlock x:Name="firstname" Text="{Binding FirstName}"    /> 
  5.                 <TextBlock x:Name="lastname" Text="{Binding LastName}"  Margin="150,0,0,0"   /> 
  6.             </Grid> 
  7.         </DataTemplate>                       
  8.     </UserControl.Resources> 
  9.     <Grid x:Name="LayoutRoot" Background="White"> 
  10.         <ListBox Height="100" HorizontalAlignment="Left" Margin="114,130,0,0" Name="listBox1" VerticalAlignment="Top" Width="220"  ItemsSource="{Binding Mode=OneWay}" ItemTemplate="{StaticResource template}" /> 
  11.     </Grid> 

其实可以看出,在静态资源中,对于数据模板的定义仍然采用的之前与数组对象同样的绑定方式,只不过它满足了更高的需求
最后再运行一下,结果如下:

QQ截图未命名1

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作