[原]WPF编程经常遇到一个问题:

      某个数组己绑定到主界面某控件中,然后在后台程序中需要对数组增(减)数据,然后程序就会报错,

程序提示:该类型的CollectionView 不支持从调度程序线程以外的线程对其SourceCollection进行的更改。

如下图所示:

[WPF]绑定到界面的数组不支持调度线程以外对其更改的办法

既然不能这样操作,就得想一个办法来解决,现在先把把出现错误的程序全部列出来,然后再来根据解决办法进行修改,

本测试程序先建一个学生类:

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()
        { }
        
    }
}
学生类代码View Code

相关文章:

  • 2021-11-18
  • 2021-04-20
  • 2022-12-23
  • 2021-05-26
  • 2022-02-22
  • 2021-08-06
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-02-18
  • 2021-12-31
  • 2022-12-23
  • 2022-12-23
  • 2021-08-24
相关资源
相似解决方案