对Silverlight中ObservableCollection自己的一点理解 .

来源:互联网 发布:parsley.js 中文提示 编辑:程序博客网 时间:2024/05/01 10:45
本篇学习了ObservableCollection<T>相关知识,因为在项目开发中我碰到一些问题,后来发现时我的理解偏差!所以做下笔记!

 

(一)代码:

 
  1.  
    [c-sharp] view plaincopyprint?
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Net;  
    5. using System.Windows;  
    6. using System.Windows.Controls;  
    7. using System.Windows.Documents;  
    8. using System.Windows.Input;  
    9. using System.Windows.Media;  
    10. using System.Windows.Media.Animation;  
    11. using System.Windows.Shapes;  
    12. //添加命名空间   
    13. using System.Collections.ObjectModel;  
    14. using System.ComponentModel;  
    15.   
    16. namespace CatalogTest  
    17. {  
    18.     public partial class ObservableCollectionVSList : UserControl  
    19.     {  
    20.         ObservableCollection<Student> students = new ObservableCollection<Student>();  
    21.         //List<Student> students = new List<Student>();  
    22.         Student selectedStudent = null;  
    23.   
    24.         public ObservableCollectionVSList()  
    25.         {  
    26.             InitializeComponent();  
    27.             this.Loaded += new RoutedEventHandler(ObservableCollectionVSList_Loaded);  
    28.         }  
    29.   
    30.         void ObservableCollectionVSList_Loaded(object sender, RoutedEventArgs e)  
    31.         {  
    32.             Student student1 = new Student() { StudentID = "001", StudentName = "张三" };  
    33.             Student student2 = new Student() { StudentID = "002", StudentName = "李四" };  
    34.             Student student3 = new Student() { StudentID = "003", StudentName = "王五" };  
    35.             students.Add(student1);  
    36.             students.Add(student2);  
    37.             students.Add(student3);  
    38.             //绑定   
    39.             listBox1.ItemsSource = students;  
    40.             listBox1.DisplayMemberPath = "StudentName";  
    41.             //注册选择项事件   
    42.             this.listBox1.SelectionChanged += new SelectionChangedEventHandler(listBox1_SelectionChanged);             
    43.   
    44.         }        
    45.   
    46.         void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)  
    47.         {              
    48.             selectedStudent = this.listBox1.SelectedItem as Student;              
    49.         }  
    50.   
    51.         //删除   
    52.         private void btnDel_Click(object sender, RoutedEventArgs e)  
    53.         {  
    54.             if (selectedStudent != null)  
    55.             {  
    56.                 students.Remove(selectedStudent);  
    57.             }            
    58.   
    59.         }  
    60.         //修改   
    61.         private void btnEdit_Click(object sender, RoutedEventArgs e)  
    62.         {  
    63.             if (selectedStudent != null)  
    64.             {  
    65.                 int myIndex = students.IndexOf(selectedStudent);  
    66.                 students[myIndex].StudentName = "我改名了!";  
    67.             }  
    68.         }  
    69.         //添加   
    70.         private void btnAdd_Click(object sender, RoutedEventArgs e)  
    71.         {  
    72.             Student student = new Student() { StudentID="009",StudentName="Joetao"};             
    73.             students.Add(student);  
    74.         }  
    75.     }  
    76.     public class Student  
    77.     {  
    78.         public string  StudentID { getset; }  
    79.         public string  StudentName { getset; }  
    80.     }     
    81.   
    82.     //分别采用 ObservableCollection<Student>与List<Student>作为绑定数据源  
    83.     //当我们用List<T>作为数据源绑定UI控件时:当做增删改操作来修改students绑定数据源时,数据源都不能通知UI更新。  
    84.     //当我们用ObservableCollection<T>作为数据绑定UI控件时:当做增删改操作来修改students绑定数据源时,Add()和Remove()操作修改的数据源能通知UI更新,而改操作不能更新UI.  
    85.     //                               //这一点正说明了MSDN上对ObservableCollection<T>类介绍:"表示一个动态数据集合,在添加项、移除项或刷新整个列表时,此集合将提供通知。"  
    86.      
    87.     //我在处理这个问题理解偏差:   
    88.     //在一开始的时候我对这句话:“实现您自己的集合之前,应考虑使用 ObservableCollection<T> 类,该类具有 INotifyCollectionChanged 和 INotifyPropertyChanged 的内置实现。“  
    89.     //被我理解为了只要用了ObservableCollection<T>,这个类集合以及类集合中的所有成员属性就具有更改通知的功能,这个理解是错误。ObservableCollection<T>只是针对T类型而言,并非给予了  
    90.     //类成员属性更改通知的功能,要想类成员属性具有更改UI功能还得让类继承INotifyPropertyChanged接口,并用 (NotifyCollectionChangedEventArgs) 的事件数据报告有关集合更改的特性的信息  
    91.     }  

(二)实现类成员更改通知UI:

          要想实现类属性值修改,我们必须修改Student类,如下:

 

 

  

[c-sharp] view plaincopyprint?
  1. public class Student : INotifyPropertyChanged  
  2. {  
  3.     private string studentID;  
  4.     public string StudentID  
  5.     {  
  6.         get { return studentID; }  
  7.         set  
  8.         {  
  9.             studentID = value;  
  10.             NotifyPropertyChange("StudentID");  
  11.         }  
  12.     }  
  13.     private string studentName;  
  14.     public string StudentName  
  15.     {  
  16.         get { return studentName; }  
  17.         set  
  18.         {  
  19.             studentName = value;  
  20.             NotifyPropertyChange("StudentName");  
  21.         }  
  22.     }  
  23.   
  24.     public event PropertyChangedEventHandler PropertyChanged;  
  25.     private void NotifyPropertyChange(string propertyName)  
  26.     {  
  27.         if (PropertyChanged != null)  
  28.         {  
  29.             PropertyChanged(thisnew PropertyChangedEventArgs(propertyName));  
  30.         }  
  31.     }  
  32.   
  33. }  

(三)总结:

          本篇学习了 ObservableCollection<T>与List<T>作为绑定数据源的不同,实例充分说明了 ObservableCollection<T>在Silverlight中作为绑定数据源的优势! 并例举了自己最初对ObservableCollection<T>的误解。并说明了怎样实现类属性成员的修改更改UI的实现方法!这里绑定可以是OneTime,OneWay,TwoWay.

原创粉丝点击