INotifyPropertyChanged接口的PropertyChanged 事件
INotifyPropertyChanged 接口用于向客户端(通常是执行绑定的客户端)发出某一属性值已更改的通知。
例如,考虑一个带有名为 FirstName 属性的 Person 对象。 若要提供一般性属性更改通知,则 Person 类型实现 INotifyPropertyChanged 接口并在 FirstName更改时引发 PropertyChanged 事件。
若要在将客户端与数据源进行绑定时发出更改通知,则绑定类型应具有下列任一功能:
实现 INotifyPropertyChanged 接口(首选)。
为绑定类型的每个属性提供更改事件。
上述这两个功能不要同时实现。
下面的代码示例演示如何实现 INotifyPropertyChanged 接口。 在运行此示例时,您将注意到绑定的 DataGridView 控件无需重置绑定即能反映数据源中的更改。
如果使用 CallerMemberName 属性,对 NotifyPropertyChanged 方法不必指定属性名称作为字符串参数。 有关更多信息,请参见 调用方信息(C# 和 Visual Basic)。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Windows.Forms; // Change the namespace to the project name.
namespace TestNotifyPropertyChangedCS
{
// This form demonstrates using a BindingSource to bind
// a list to a DataGridView control. The list does not
// raise change notifications. However the DemoCustomer type
// in the list does.
public partial class Form1 : Form
{
// This button causes the value of a list element to be changed.
private Button changeItemBtn = new Button(); // This DataGridView control displays the contents of the list.
private DataGridView customersDataGridView = new DataGridView(); // This BindingSource binds the list to the DataGridView control.
private BindingSource customersBindingSource = new BindingSource(); public Form1()
{
InitializeComponent(); // Set up the "Change Item" button.
this.changeItemBtn.Text = "Change Item";
this.changeItemBtn.Dock = DockStyle.Bottom;
this.changeItemBtn.Click +=
new EventHandler(changeItemBtn_Click);
this.Controls.Add(this.changeItemBtn); // Set up the DataGridView.
customersDataGridView.Dock = DockStyle.Top;
this.Controls.Add(customersDataGridView); this.Size = new Size(, );
} private void Form1_Load(object sender, EventArgs e)
{
// Create and populate the list of DemoCustomer objects
// which will supply data to the DataGridView.
BindingList<DemoCustomer> customerList = new BindingList<DemoCustomer>();
customerList.Add(DemoCustomer.CreateNewCustomer());
customerList.Add(DemoCustomer.CreateNewCustomer());
customerList.Add(DemoCustomer.CreateNewCustomer()); // Bind the list to the BindingSource.
this.customersBindingSource.DataSource = customerList; // Attach the BindingSource to the DataGridView.
this.customersDataGridView.DataSource =
this.customersBindingSource; } // Change the value of the CompanyName property for the first
// item in the list when the "Change Item" button is clicked.
void changeItemBtn_Click(object sender, EventArgs e)
{
// Get a reference to the list from the BindingSource.
BindingList<DemoCustomer> customerList =
this.customersBindingSource.DataSource as BindingList<DemoCustomer>; // Change the value of the CompanyName property for the
// first item in the list.
customerList[].CustomerName = "Tailspin Toys";
customerList[].PhoneNumber = "(708)555-0150";
} } // This is a simple customer class that
// implements the IPropertyChange interface.
public class DemoCustomer : INotifyPropertyChanged
{
// These fields hold the values for the public properties.
private Guid idValue = Guid.NewGuid();
private string customerNameValue = String.Empty;
private string phoneNumberValue = String.Empty; public event PropertyChangedEventHandler PropertyChanged; // This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
} // The constructor is private to enforce the factory pattern.
private DemoCustomer()
{
customerNameValue = "Customer";
phoneNumberValue = "(312)555-0100";
} // This is the public factory method.
public static DemoCustomer CreateNewCustomer()
{
return new DemoCustomer();
} // This property represents an ID, suitable
// for use as a primary key in a database.
public Guid ID
{
get
{
return this.idValue;
}
} public string CustomerName
{
get
{
return this.customerNameValue;
} set
{
if (value != this.customerNameValue)
{
this.customerNameValue = value;
NotifyPropertyChanged();
}
}
} public string PhoneNumber
{
get
{
return this.phoneNumberValue;
} set
{
if (value != this.phoneNumberValue)
{
this.phoneNumberValue = value;
NotifyPropertyChanged();
}
}
}
}
}
INotifyPropertyChanged接口的PropertyChanged 事件的更多相关文章
- 转载:WPF MVVM之INotifyPropertyChanged接口的几种实现方式
原文地址:http://www.cnblogs.com/xiwang/ 序言 借助WPF/Sliverlight强大的数据绑定功能,可以比实现比MFC,WinForm更加优雅轻松的数据绑定.但是在使用 ...
- WPF MVVM之INotifyPropertyChanged接口的几种实现方式(转)
原地址:https://www.cnblogs.com/xiwang/archive/2012/11/25/2787358.html 序言 借助WPF/Sliverlight强大的数据绑定功能,可以比 ...
- 【.NET深呼吸】INotifyPropertyChanged接口的真故事
无论是在流氓腾的问问社区,还是在黑度贴吧,或是“厕所等你”论坛上,曾经看到过不少朋友讨论INotifyPropertyChanged接口.不少朋友认为该接口是为双向绑定而使用的,那么,真实的情况是这样 ...
- [译]WPF MVVM 架构 Step By Step(5)(添加actions和INotifyPropertyChanged接口)
应用不只是包含textboxs和labels,还包含actions,如按钮和鼠标事件等.接下来我们加上一些像按钮这样的UI元素来看MVVM类怎么演变的.与之前的UI相比,这次我们加上一个"C ...
- C# ListView 控件和 INotifyPropertyChanged 接口
ListView 控件和 DataGridView 控件 ListView 是跟 Winform 中 DataGridView 用法以及显示效果差不多的一个 WPF 控件,可以通过列表的方式方便的显示 ...
- INotifyPropertyChanged 接口
INotifyPropertyChanged 接口 用于向客户端(通常是执行绑定的客户端)发出某一属性值已更改的通知. 例如,考虑一个带有名为 FirstName 属性的 Person 对象.若要提供 ...
- ListView 控件和 INotifyPropertyChanged 接口
原文:ListView 控件和 INotifyPropertyChanged 接口 ListView 控件和 DataGridView 控件 ListView 是跟 Winform 中 DataGri ...
- INotifyPropertyChanged接口的实现
何时实现INotifyPropertyChanged接口 官方解释:INotifyPropertyChanged 接口用于向客户端(通常是执行绑定的客户端)发出某一属性值已更改的通知.官方解释的很模 ...
- WPF学习总结1:INotifyPropertyChanged接口的作用
在代码中经常见到这个接口,它里面有什么?它的作用是什么?它和依赖属性有什么关系? 下面就来总结回答这三个问题. 1.这个INotifyPropertyChanged接口里就一个PropertyChan ...
随机推荐
- C++函数CString类常用函数
C++ STL库里有很多与字符串操作相关的函数,熟练应用STL,字符串的处理将变得轻松.自在. 字符串截取函数: 1.CString Left( int nCount ) const; //从左 ...
- Eclipse设置默认注释
在 windows-->preferenceJava-->Code Style-->Code Templatescode-->new Java file点编辑,覆盖原文本: $ ...
- SqlServer传输数据到ORACLE,SSIS
一.配置32位ODBC 配置tnsname文件,增加ORACLE数据库 打开32位ODBC 二.创建一个PROJECT并配置数据源 1.创建一个project 三.数据传输SSIS,工作流 四.为DT ...
- Spring Framework------>version4.3.5----->Reference学习心得----->总结
1.Spring Framework概述: 有很多可用版本,网址http://projects.spring.io/spring-framework/ 2.Spring Framework ...
- char类型输出地址
问题描述: 当输出char的地址时,发现输出的是一个字符: char ch = 'a'; cout<<&ch<<endl;//a @ 因为cout得到一个char类型的 ...
- iOS常用库之Masonry
简单介绍 Masonry 源码地址:https://github.com/Masonry/Masonry Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布局 简 ...
- html之如何让文字两端对齐
text-align: justify;/*英文*/ text-align-last: justify;/*中英文*/ text-align-last: justify;亲测有效(chrome)
- HTML5 ---localStorage储存实例
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title> ...
- c++模板使用出错情况error LNK2019: unresolved external symbol "public: float __thiscall Compare<float>::min(void)" (?min@?$Compare@M@@QAEMXZ) referenced in function _main
将类模板在头文件中定义,类的成员函数在头文件中声明,头文件中只留下接口,函数的实现在另一个.cpp文件中,这样编译出来错误error LNK2019: unresolved external symb ...
- C++读写文件ofstream,ifstream,fstream)[转]
在看C++编程思想中,每个练习基本都是使用ofstream,ifstream,fstream,以前粗略知道其用法和含义,在看了几位大牛的博文后,进行整理和总结: 这里主要是讨论fstream的内容:[ ...