[原]WPF编程经常遇到一个问题:
某个数组己绑定到主界面某控件中,然后在后台程序中需要对数组增(减)数据,然后程序就会报错,
程序提示:该类型的CollectionView 不支持从调度程序线程以外的线程对其SourceCollection进行的更改。
如下图所示:
既然不能这样操作,就得想一个办法来解决,现在先把把出现错误的程序全部列出来,然后再来根据解决办法进行修改,
本测试程序先建一个学生类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; namespace WPF_test { public class student : INotifyPropertyChanged { //定义数据更改事件通知和更改的方法 public event PropertyChangedEventHandler PropertyChanged; public void up(string s) { if (this.PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(s)); } } //姓名 string _name; public string name { get { return _name; } set { _name = value; up("name"); } } //学号 string _id; public string id { get { return _id; } set { _id = value; up("name"); } } public student(string _id, string _name) { id = _id; name = _name; } public student() { } } }