INotifyPropertyChanged 接口

2016-07-24

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 }

转自微软官网技术文章:

https://msdn.microsoft.com/zh-cn/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2


技术研究方向:专注于Web(Mvc)开发框架、WinForm开发框架、项目(代码)自动化生成器、ORM等技术研究与开发应用

企业级项目经验:编务管理系统、印前管理系统、印务管理系统、图书销售管理系统、图书发行管理系统、图书馆管理系统、

数据交换平台、ERP综合管理平台



欢迎转载,请注明文章出处与链接信息。    如果文章对您有帮助,请帮忙推荐,谢谢!

撰写人:张传宁  http://www.cnblogs.com/SavionZhang                   

时间: 2024-08-07 04:31:32

INotifyPropertyChanged 接口的相关文章

INotifyPropertyChanged 接口 CallerMemberName属性

2016-07-24 调用方信息 使用调用方信息属性,可以获取关于调用方的信息传递给方法. 可以获取源代码.行号在源代码和调用方的成员名称的文件路径. 此信息用于跟踪,调试和创建诊断工具非常有用. 若要获取此信息,则使用适用于可选参数,每个都有一个默认的属性. 下表列出了 System.Runtime.CompilerServices 命名空间中定义的调用方信息属性: 特性 说明 类型 CallerFilePathAttribute 包含调用方源文件的完整路径. 这是文件路径在编译时. Stri

转载:WPF MVVM之INotifyPropertyChanged接口的几种实现方式

原文地址:http://www.cnblogs.com/xiwang/ 序言 借助WPF/Sliverlight强大的数据绑定功能,可以比实现比MFC,WinForm更加优雅轻松的数据绑定.但是在使用WPF/Silverlight绑定时,有件事情是很苦恼的:当ViewModel对象放生改变,需要通知UI.我们可以让VM对象实现INotifyPropertyChanged接口,通过事件来通知UI.但问题就出现这里…… 一,描述问题 情形:现在需要将一个Person对象的Name熟悉双向绑定到UI中

INotifyPropertyChanged接口的实现

何时实现INotifyPropertyChanged接口 官方解释:INotifyPropertyChanged  接口用于向客户端(通常是执行绑定的客户端)发出某一属性值已更改的通知.官方解释的很模糊,估计是个人看了都不知道到底什么时候需要实现INotifyPropertyChanged接口.小梦通过实际测试给出明确结论: 首先:OneTime模式:毫无意义,因为它的绑定只有初始时候绑定一次,根本谈不上改变!自然也就谈不上实现INotifyPropertyChanged接口. 然后是OneWa

WPF系列之三:实现类型安全的INotifyPropertyChanged接口,可以不用“Magic string” 么?

通常实现INotifyPropertyChanged接口很简单,为你的类只实现一个PropertyChanged 的Event就可以了. 例如实现一个简单的ViewModel1类: public class ViewModel1 : INotifyPropertyChanged { private string _data; public string Data { get { return _data; } set { if (_data == value) return; _data = v

INotifyPropertyChanged接口的详细说明

在windows phone开发8.1:数据绑定中,我们了解了数据绑定的基本知识.今后几篇文章会继续深入了解数据绑定.今天我们来看在数据绑定中十分重要的INotifyPropertyChanged接口的实现. 何时实现INotifyPropertyChanged接口 官方解释:INotifyPropertyChanged  接口用于向客户端(通常是执行绑定的客户端)发出某一属性值已更改的通知.官方解释的很模糊,估计是个人看了都不知道到底什么时候需要实现INotifyPropertyChanged

【.NET深呼吸】INotifyPropertyChanged接口的真故事

无论是在流氓腾的问问社区,还是在黑度贴吧,或是“厕所等你”论坛上,曾经看到过不少朋友讨论INotifyPropertyChanged接口.不少朋友认为该接口是为双向绑定而使用的,那么,真实的情况是这样的吗? INotifyPropertyChanged接口位于System.ComponentModel命名空间,在该命名空间下还有另一个接口:INotifyPropertyChanging.INotifyPropertyChanging接口定义了PropertyChanging事件,应该在在属性值正

INotifyPropertyChanged接口的PropertyChanged 事件

INotifyPropertyChanged 接口用于向客户端(通常是执行绑定的客户端)发出某一属性值已更改的通知. 例如,考虑一个带有名为 FirstName 属性的 Person 对象. 若要提供一般性属性更改通知,则 Person 类型实现 INotifyPropertyChanged 接口并在 FirstName更改时引发 PropertyChanged 事件. 若要在将客户端与数据源进行绑定时发出更改通知,则绑定类型应具有下列任一功能: 实现 INotifyPropertyChange

MVVM设计模式基础知识--INotifyPropertyChanged接口

在.NET平台上,数据绑定是一项令人十分愉快的技术.利用数据绑定能减少代码,简化控制逻辑. 通常,可以将某个对象的一个属性绑定到一个可视化的控件上,当属性值改变时,控件上的显示数据也随之发生变化.要实现这一功能,只需要为自定义对象实现 INotifyPropertyChanged 接口即可.此接口中定义了 PropertyChanged 事件,我们只需在属性值改变时触发该事件即可. INotifyPropertyChanged 接口是 WPF/Silverlight 开发中非常重要的接口, 它构

[译]WPF MVVM 架构 Step By Step(5)(添加actions和INotifyPropertyChanged接口)

原文:[译]WPF MVVM 架构 Step By Step(5)(添加actions和INotifyPropertyChanged接口) 应用不只是包含textboxs和labels,还包含actions,如按钮和鼠标事件等.接下来我们加上一些像按钮这样的UI元素来看MVVM类怎么演变的.与之前的UI相比,这次我们加上一个"Cal Tax"按钮,当我们点击这个依赖于“sales amount”的按钮时,它会计算税费并显示在同窗口内. 为了完成所述的功能,我们先在Model类中添加一个