未实现INotifyPropertyChanged时,未反映数据更改

时间:2015-04-20 08:22:34

标签: c# datagridview inotifypropertychanged

首先看下面的代码哪个工作正常,当我们更新实体时,更改会自动反映在网格中,但是当我评论此INotifyPropertyChanged以及与通知属性更改相关的所有其他代码时,网格没有得到更改

所以我想知道INotifyPropertyChanged扮演什么样的角色? INotifyPropertyChanged如何与网格通信以显示或反映chnage? 请帮助我理解grid and INotifyPropertyChanged之间的沟通。感谢

namespace PatternSearch
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void btnBindData_Click(object sender, EventArgs e)
        {
            BindingList<Car> cars = new BindingList<Car>();

            cars.Add(new Car("Ford", "Mustang", 1967));
            cars.Add(new Car("Shelby AC", "Cobra", 1965));
            cars.Add(new Car("Chevrolet", "Corvette Sting Ray", 1965));

            dataGridView1.DataSource = cars;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (dataGridView1.DataSource != null)
            {
                BindingList<Car> cars = dataGridView1.DataSource as BindingList<Car>;
                cars.Where(d => d.Make == "Ford").First().Make = "My Ford000";
            }
            else
                MessageBox.Show("Grid has no data");
        }
    }


    public class Car : INotifyPropertyChanged
    {
        private string _make;
        private string _model;
        private int _year;

        public event PropertyChangedEventHandler PropertyChanged;

        public Car(string make, string model, int year)
        {
            _make = make;
            _model = model;
            _year = year;
        }

        public string Make
        {
            get { return _make; }
            set
            {
                _make = value;
                this.NotifyPropertyChanged("Make");
            }
        }

        public string Model
        {
            get { return _model; }
            set
            {
                _model = value;
                this.NotifyPropertyChanged("Model");
            }
        }

        public int Year
        {
            get { return _year; }
            set
            {
                _year = value;
                this.NotifyPropertyChanged("Year");
            }
        }

        private void NotifyPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}

这里是我评论INotifyPropertyChanged及其所有相关代码的代码

namespace PatternSearch
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void btnBindData_Click(object sender, EventArgs e)
        {
            BindingList<Car> cars = new BindingList<Car>();

            cars.Add(new Car("Ford", "Mustang", 1967));
            cars.Add(new Car("Shelby AC", "Cobra", 1965));
            cars.Add(new Car("Chevrolet", "Corvette Sting Ray", 1965));

            dataGridView1.DataSource = cars;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (dataGridView1.DataSource != null)
            {
                BindingList<Car> cars = dataGridView1.DataSource as BindingList<Car>;
                cars.Where(d => d.Make == "Ford").First().Make = "My Ford000";
            }
            else
                MessageBox.Show("Grid has no data");
        }
    }


    public class Car //: INotifyPropertyChanged
    {
        private string _make;
        private string _model;
        private int _year;

        //public event PropertyChangedEventHandler PropertyChanged;

        public Car(string make, string model, int year)
        {
            _make = make;
            _model = model;
            _year = year;
        }

        public string Make
        {
            get { return _make; }
            set
            {
                _make = value;
                //this.NotifyPropertyChanged("Make");
            }
        }

        public string Model
        {
            get { return _model; }
            set
            {
                _model = value;
                //this.NotifyPropertyChanged("Model");
            }
        }

        public int Year
        {
            get { return _year; }
            set
            {
                _year = value;
                //this.NotifyPropertyChanged("Year");
            }
        }

        private void NotifyPropertyChanged(string name)
        {
            //if (PropertyChanged != null)
            //    PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}

1 个答案:

答案 0 :(得分:1)

网格检查对象是否实现了INotifyPropertyChanged。如果是,则订阅PropertyChanged事件(接口的唯一成员),以通知值已更改的属性的名称。

更好的NotifyPropertyChanged实现将是:

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
    var handler = PropertyChanged;
    if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}

并且属性看起来像:

public int Year
{
    get { return _year; }
    set
    {
        _year = value;
        OnPropertyChanged();
    }
}

通过这种方式,代码可以更好地修复属性名称重构。