【转】C#-INotifyPropertyChanged(解决数据绑定的界面刷新问题)

C#-INotifyPropertyChanged(解决数据绑定的界面刷新问题)

 

最近做项目用到DataGridView,用它绑定数据源后,如果数据源中的数据修改无法及时刷新到控件上,必须切换单元格的焦点才能导致刷新显示新数值,通过查官方文档,用INotifyPropertyChanged解决此问题。

案例如下:

首先定义一个类:Customer

    public class Customer
    {
        public string CustomerName { get; set; }
        public string PhoneNumber { get; set; }
    }

创建一个绑定数据集:

private BindingList<Customer> customers = new BindingList<Customer>();

向数据集中添加数据并绑定到控件:

customers.Add(new Customer() { CustomerName = "张三", PhoneNumber = "010-5263" });
customers.Add(new Customer() { CustomerName = "李四", PhoneNumber = "010-8823" });
dataGridView1.DataSource = customers;

修改数据源:

customers[0].CustomerName = "王五";

此时,dataGridView1中没有及时刷新显示新数据。因为数据修改时并没有通知到控件,为此让Customer实现INotifyPropertyChanged接口,该接口只包含一个事件,当属性值改变时,触发该事件,从而导致注册该事件的方法被执行,达到通知控件的目的。这是通过事件实现观察者模式。

Customer实现INotifyPropertyChanged接口:

复制代码
复制代码
    public class Customer:INotifyPropertyChanged
    {
        private string _customerName;
        private string _phoneNumber;
    </span><span style="color: rgba(0, 0, 255, 1);">public</span> <span style="color: rgba(0, 0, 255, 1);">string</span><span style="color: rgba(0, 0, 0, 1);"> CustomerName
    {
        </span><span style="color: rgba(0, 0, 255, 1);">get</span> { <span style="color: rgba(0, 0, 255, 1);">return</span><span style="color: rgba(0, 0, 0, 1);"> _customerName; }
        </span><span style="color: rgba(0, 0, 255, 1);">set</span><span style="color: rgba(0, 0, 0, 1);">
        {
            </span><span style="color: rgba(0, 0, 255, 1);">if</span>(_customerName!=<span style="color: rgba(0, 0, 0, 1);">value)
            {
                _customerName </span>=<span style="color: rgba(0, 0, 0, 1);"> value;
                PropertyChanged(</span><span style="color: rgba(0, 0, 255, 1);">this</span>, <span style="color: rgba(0, 0, 255, 1);">new</span> PropertyChangedEventArgs(<span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">CustomerName</span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(0, 0, 0, 1);">));
            }
        }
    }


    </span><span style="color: rgba(0, 0, 255, 1);">public</span> <span style="color: rgba(0, 0, 255, 1);">string</span><span style="color: rgba(0, 0, 0, 1);"> PhoneNumber
    {
        </span><span style="color: rgba(0, 0, 255, 1);">get</span> { <span style="color: rgba(0, 0, 255, 1);">return</span><span style="color: rgba(0, 0, 0, 1);"> _phoneNumber; }
        </span><span style="color: rgba(0, 0, 255, 1);">set</span><span style="color: rgba(0, 0, 0, 1);">
        {
            </span><span style="color: rgba(0, 0, 255, 1);">if</span> (_phoneNumber !=<span style="color: rgba(0, 0, 0, 1);"> value)
            {
                _phoneNumber </span>=<span style="color: rgba(0, 0, 0, 1);"> value;
                PropertyChanged(</span><span style="color: rgba(0, 0, 255, 1);">this</span>, <span style="color: rgba(0, 0, 255, 1);">new</span> PropertyChangedEventArgs(<span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">PhoneNumber</span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(0, 0, 0, 1);">));
            }
        }
    }

    </span><span style="color: rgba(0, 0, 255, 1);">public</span> <span style="color: rgba(0, 0, 255, 1);">event</span> PropertyChangedEventHandler PropertyChanged = <span style="color: rgba(0, 0, 255, 1);">delegate</span><span style="color: rgba(0, 0, 0, 1);"> { };
}</span></pre>
复制代码
复制代码

此时,再次执行修改数据源的代码,控件会及时刷新数据显示,因为属性值改变时通知了控件进行相应的处理。

如果在数据源中添加或删除一条数据,控件也会及时刷新显示,怎么回事?原来BindingList<T>实现了IBindingList接口,该接口声明了事件 ListChangedEventHandler,BindingList<T>在添加或删除记录时,触发该事件。显然,控件在绑定数据集(customers)时,订阅了该事件。

转自 https://www.cnblogs.com/guofeiji/p/5277834.html
posted @   CastleWu  阅读(471)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
点击右上角即可分享
微信分享提示