INotifyPropertyChanged 接口
INotifyPropertyChanged 接口
用于向客户端(通常是执行绑定的客户端)发出某一属性值已更改的通知。
例如,考虑一个带有名为 FirstName 属性的 Person 对象。若要提供一般性属性更改通知,则 Person 类型实现 INotifyPropertyChanged 接口并在 FirstName 更改时引发 PropertyChanged 事件。
若要在将客户端与数据源进行绑定时发出更改通知,则绑定类型应具有下列任一功能:
-
实现 INotifyPropertyChanged 接口(首选)。
-
为绑定类型的每个属性提供更改事件。
上述这两个功能不要同时实现。
下面的代码示例演示如何实现 INotifyPropertyChanged 接口。在运行此示例时,您将注意到绑定的 DataGridView 控件无需重置绑定即能反映数据源中的更改。如果使用 CallerMemberName 属性,对 NotifyPropertyChanged 方法不必指定属性名称作为字符串参数。有关详细信息,请参阅Caller Information (C# and Visual Basic)。
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Drawing; 5 using System.Runtime.CompilerServices; 6 using System.Windows.Forms; 7 8 // Change the namespace to the project name. 9 namespace TestNotifyPropertyChangedCS 10 { 11 // This form demonstrates using a BindingSource to bind 12 // a list to a DataGridView control. The list does not 13 // raise change notifications. However the DemoCustomer type 14 // in the list does. 15 public partial class Form1 : Form 16 { 17 // This button causes the value of a list element to be changed. 18 private Button changeItemBtn = new Button(); 19 20 // This DataGridView control displays the contents of the list. 21 private DataGridView customersDataGridView = new DataGridView(); 22 23 // This BindingSource binds the list to the DataGridView control. 24 private BindingSource customersBindingSource = new BindingSource(); 25 26 public Form1() 27 { 28 InitializeComponent(); 29 30 // Set up the "Change Item" button. 31 this.changeItemBtn.Text = "Change Item"; 32 this.changeItemBtn.Dock = DockStyle.Bottom; 33 this.changeItemBtn.Click += 34 new EventHandler(changeItemBtn_Click); 35 this.Controls.Add(this.changeItemBtn); 36 37 // Set up the DataGridView. 38 customersDataGridView.Dock = DockStyle.Top; 39 this.Controls.Add(customersDataGridView); 40 41 this.Size = new Size(400, 200); 42 } 43 44 private void Form1_Load(object sender, EventArgs e) 45 { 46 // Create and populate the list of DemoCustomer objects 47 // which will supply data to the DataGridView. 48 BindingList<DemoCustomer> customerList = new BindingList<DemoCustomer>(); 49 customerList.Add(DemoCustomer.CreateNewCustomer()); 50 customerList.Add(DemoCustomer.CreateNewCustomer()); 51 customerList.Add(DemoCustomer.CreateNewCustomer()); 52 53 // Bind the list to the BindingSource. 54 this.customersBindingSource.DataSource = customerList; 55 56 // Attach the BindingSource to the DataGridView. 57 this.customersDataGridView.DataSource = 58 this.customersBindingSource; 59 60 } 61 62 // Change the value of the CompanyName property for the first 63 // item in the list when the "Change Item" button is clicked. 64 void changeItemBtn_Click(object sender, EventArgs e) 65 { 66 // Get a reference to the list from the BindingSource. 67 BindingList<DemoCustomer> customerList = 68 this.customersBindingSource.DataSource as BindingList<DemoCustomer>; 69 70 // Change the value of the CompanyName property for the 71 // first item in the list. 72 customerList[0].CustomerName = "Tailspin Toys"; 73 customerList[0].PhoneNumber = "(708)555-0150"; 74 } 75 76 } 77 78 // This is a simple customer class that 79 // implements the IPropertyChange interface. 80 public class DemoCustomer : INotifyPropertyChanged 81 { 82 // These fields hold the values for the public properties. 83 private Guid idValue = Guid.NewGuid(); 84 private string customerNameValue = String.Empty; 85 private string phoneNumberValue = String.Empty; 86 87 public event PropertyChangedEventHandler PropertyChanged; 88 89 // This method is called by the Set accessor of each property. 90 // The CallerMemberName attribute that is applied to the optional propertyName 91 // parameter causes the property name of the caller to be substituted as an argument. 92 private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 93 { 94 if (PropertyChanged != null) 95 { 96 PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 97 } 98 } 99 100 // The constructor is private to enforce the factory pattern. 101 private DemoCustomer() 102 { 103 customerNameValue = "Customer"; 104 phoneNumberValue = "(312)555-0100"; 105 } 106 107 // This is the public factory method. 108 public static DemoCustomer CreateNewCustomer() 109 { 110 return new DemoCustomer(); 111 } 112 113 // This property represents an ID, suitable 114 // for use as a primary key in a database. 115 public Guid ID 116 { 117 get 118 { 119 return this.idValue; 120 } 121 } 122 123 public string CustomerName 124 { 125 get 126 { 127 return this.customerNameValue; 128 } 129 130 set 131 { 132 if (value != this.customerNameValue) 133 { 134 this.customerNameValue = value; 135 NotifyPropertyChanged(); 136 } 137 } 138 } 139 140 public string PhoneNumber 141 { 142 get 143 { 144 return this.phoneNumberValue; 145 } 146 147 set 148 { 149 if (value != this.phoneNumberValue) 150 { 151 this.phoneNumberValue = value; 152 NotifyPropertyChanged(); 153 } 154 } 155 } 156 } 157 }
转自微软官网技术文章:
成在管理,败在经验;嬴在选择,输在不学! 贵在坚持!
个人作品
BIMFace.SDK.NET
开源地址:https://gitee.com/NAlps/BIMFace.SDK
系列博客:https://www.cnblogs.com/SavionZhang/p/11424431.html
系列视频:https://www.cnblogs.com/SavionZhang/p/14258393.html
技术栈
1、AI、DeepSeek、MiniMax、通义千问
2、Visual Studio、.NET Core/.NET、MVC、Web API、RESTful API、gRPC、SignalR、Java、Python
3、jQuery、Vue.js、Bootstrap、ElementUI
4、数据库:分库分表、读写分离、SQLServer、MySQL、PostgreSQL、Redis、MongoDB、ElasticSearch、达梦DM、GaussDB、OpenGauss
5、架构:DDD、ABP、SpringBoot、jFinal
6、环境:跨平台、Windows、Linux
7、移动App:Android、IOS、HarmonyOS、微信小程序、钉钉、uni-app、MAUI
8、分布式、高并发、云原生、微服务、Docker、CI/CD、DevOps、K8S;Dapr、RabbitMQ、Kafka、RPC、Elasticsearch
欢迎关注作者头条号 张传宁IT讲堂,获取更多IT文章、视频等优质内容。
出处:www.cnblogs.com/SavionZhang
作者:张传宁 技术顾问、培训讲师、微软MCP、系统架构设计师、系统集成项目管理工程师、科技部创新工程师。
专注于企业级通用开发平台、工作流引擎、自动化项目(代码)生成器、SOA 、DDD、 云原生(Docker、微服务、DevOps、CI/CD);PDF、CAD、BIM 审图等研究与应用。
多次参与电子政务、图书教育、生产制造等企业级大型项目研发与管理工作。
熟悉中小企业软件开发过程:可行调研、需求分析、架构设计、编码测试、实施部署、项目管理。通过技术与管理帮助中小企业实现互联网转型升级全流程解决方案。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
如有问题,可以通过邮件905442693@qq.com联系。共同交流、互相学习。
如果您觉得文章对您有帮助,请点击文章右下角【推荐】。您的鼓励是作者持续创作的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?