首先看下面的代码哪个工作正常,当我们更新实体时,更改会自动反映在网格中,但是当我评论此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));
}
}
}
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));
}
}
}
答案 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();
}
}
通过这种方式,代码可以更好地修复属性名称重构。