wpf学习之ObservableCollection<T>相关知识

本篇学习了ObservableCollection<T>相关知识,因为在项目开发中我碰到一些问题,后来发现时我的理解偏差!所以做下笔记!

(一)代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
//添加命名空间
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace CatalogTest
{
    public partial class ObservableCollectionVSList : UserControl
    {
        ObservableCollection<Student> students = new ObservableCollection<Student>();
        //List<Student> students = new List<Student>();
        Student selectedStudent = null;

        public ObservableCollectionVSList()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ObservableCollectionVSList_Loaded);
        }

        void ObservableCollectionVSList_Loaded(object sender, RoutedEventArgs e)
        {
            Student student1 = new Student() { StudentID = "001", StudentName = "张三" };
            Student student2 = new Student() { StudentID = "002", StudentName = "李四" };
            Student student3 = new Student() { StudentID = "003", StudentName = "王五" };
            students.Add(student1);
            students.Add(student2);
            students.Add(student3);
            //绑定
            listBox1.ItemsSource = students;
            listBox1.DisplayMemberPath = "StudentName";
            //注册选择项事件
            this.listBox1.SelectionChanged += new SelectionChangedEventHandler(listBox1_SelectionChanged);           

        }      

        void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {            
            selectedStudent = this.listBox1.SelectedItem as Student;            
        }

        //删除
        private void btnDel_Click(object sender, RoutedEventArgs e)
        {
            if (selectedStudent != null)
            {
                students.Remove(selectedStudent);
            }          

        }
        //修改
        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            if (selectedStudent != null)
            {
                int myIndex = students.IndexOf(selectedStudent);
                students[myIndex].StudentName = "我改名了!";
            }
        }
        //添加
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            Student student = new Student() { StudentID="009",StudentName="Joetao"};           
            students.Add(student);
        }
    }
    public class Student
    {
        public string  StudentID { get; set; }
        public string  StudentName { get; set; }
    }   

    //分别采用 ObservableCollection<Student>与List<Student>作为绑定数据源
    //当我们用List<T>作为数据源绑定UI控件时:当做增删改操作来修改students绑定数据源时,数据源都不能通知UI更新。
    //当我们用ObservableCollection<T>作为数据绑定UI控件时:当做增删改操作来修改students绑定数据源时,Add()和Remove()操作修改的数据源能通知UI更新,而改操作不能更新UI.
    //                               //这一点正说明了MSDN上对ObservableCollection<T>类介绍:"表示一个动态数据集合,在添加项、移除项或刷新整个列表时,此集合将提供通知。"
   
    //我在处理这个问题理解偏差:
    //在一开始的时候我对这句话:“实现您自己的集合之前,应考虑使用 ObservableCollection<T> 类,该类具有 INotifyCollectionChanged 和 INotifyPropertyChanged 的内置实现。“
    //被我理解为了只要用了ObservableCollection<T>,这个类集合以及类集合中的所有成员属性就具有更改通知的功能,这个理解是错误。ObservableCollection<T>只是针对T类型而言,并非给予了
    //类成员属性更改通知的功能,要想类成员属性具有更改UI功能还得让类继承INotifyPropertyChanged接口,并用 (NotifyCollectionChangedEventArgs) 的事件数据报告有关集合更改的特性的信息
    }


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

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

    public class Student : INotifyPropertyChanged
    {
        private string studentID;
        public string StudentID
        {
            get { return studentID; }
            set
            {
                studentID = value;
                NotifyPropertyChange("StudentID");
            }
        }
        private string studentName;
        public string StudentName
        {
            get { return studentName; }
            set
            {
                studentName = value;
                NotifyPropertyChange("StudentName");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChange(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }

(三)总结:
          本篇学习了 ObservableCollection<T>与List<T>作为绑定数据源的不同,实例充分说明了 ObservableCollection<T>在Silverlight中作为绑定数据源的优势! 并例举了自己最初对ObservableCollection<T>的误解。并说明了怎样实现类属性成员的修改更改UI的实现方法!这里绑定可以是OneTime,OneWay,TwoWay.具体    理解可以看我的另一篇文章Silverlight中OneTime,OneWay,TwoWay及INotifyPropertyChanged 接口的理解

(四)
数据层已经变了,为何有时更新不了界面,因为取不到界面

(五)
线程内进行
for (int i = 0; i < vCancels.Count; i++)
                {
                    Mgr.TransactionService.CancelOrder("F", vCancels[i].OrderId, vCancels[i].ContractNo, vCancels[i].OperatorNo);
                }


不要涉及到任何界面的,开始用界面的iTransactionService
iTransactionService = Mgr.TransactionService;
线程中for循环还是会卡界面

猜你喜欢

转载自jcair.iteye.com/blog/2205311