INotifyPropertyChanged

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    
public partial class Form1 : Form
    {
        
public Form1()
        {
            InitializeComponent();
        }

        
private void button1_Click(object sender, EventArgs e)
        {
            BindingList
<DemoCustomer> customerList = bindingSource1.DataSource as BindingList<DemoCustomer>;

            customerList[
0].Name = "aa";
            bindingSource1.ResetItem(
0);
        }

        
private void Form1_Load(object sender, EventArgs e)
        {
            BindingList
<DemoCustomer> customer = new BindingList<DemoCustomer>();
            customer.Add(DemoCustomer.CreateCustomer());
            customer.Add(DemoCustomer.CreateCustomer());
            customer.Add(DemoCustomer.CreateCustomer());

            bindingSource1.DataSource 
= customer;
            dataGridView1.DataSource 
= bindingSource1;
        }
    }
    
public class DemoCustomer : INotifyPropertyChanged
    {
        
private Guid idValue = Guid.NewGuid();
        
private string name = string.Empty;

        
public string Name
        {
            
get { return name; }
            
set {
                
if (value != this.name)
                {
                    name 
= value;

                    NotifyPropertyChanged(
"customer");
                }
            }
        }
        
private string phone = string.Empty;

        
public string Phone
        {
            
get { return phone; }
            
set {
                
if (value != this.phone)
                {
                    phone 
= value;
                    NotifyPropertyChanged(
"phone");
                }
            }
        }

        
private void NotifyPropertyChanged(string info)
        {
            
if (PropertyChanged != null)
            {
                PropertyChanged(
thisnew PropertyChangedEventArgs(info));
            }
        }
        
//单例
        private DemoCustomer()
        {
            name 
= "customer";
            phone 
= "(555)1234567";
        }
        
public static DemoCustomer CreateCustomer()
        {
            
return new DemoCustomer();
        }

        
public Guid Id
        {
            
get
            {
                
return this.idValue;
            }
        }
        
#region INotifyPropertyChanged 成员

        
public event PropertyChangedEventHandler PropertyChanged;

        
#endregion
    }
}


原文地址:https://www.cnblogs.com/chenqingwei/p/1863893.html