向客户端发出某一属性值已更改的通知。

名空间:System.ComponentModel
程序集:  
System(在 System.dll 中)

语法
public interface INotifyPropertyChanged
public interface class INotifyPropertyChanged


备注
INotifyPropertyChanged
接口用于向客户端(通常是执行绑定的客户端)发出某一属性值已更改的通知。

例如,考虑一个带有名为 FirstName 属性的 Person 对象。
若要提供一般性属性更改通知,则
Person 类型实现 INotifyPropertyChanged 接口并在 FirstName 更改时引发 PropertyChanged 事件。

若要在将客户端与数据源进行绑定时发出更改通知,则绑定类型应具有下列任一功能:

  • 实现 INotifyPropertyChanged 接口(首选)。

  • 为绑定类型的每个属性提供更改事件。

不执行上述这两个功能。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Diagnostics;
using System.Drawing;
using System.Data.SqlClient;
using System.Windows.Forms;

// 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 class Form1 : System.Windows.Forms.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()
    {
        // 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(800, 200);
        this.Load += new EventHandler(Form1_Load);
    }

private void Form1_Load(System.Object sender, System.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[0].CustomerName = "Tailspin Toys";
        customersBindingSource.ResetItem(0);
    }

[STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }
}

// 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;

private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

// The constructor is private to enforce the factory pattern.
    private DemoCustomer()
    {
        customerNameValue = "Customer";
        phoneNumberValue = "(555)555-5555";
    }

// 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("CustomerName");
            }
        }
    }

public string PhoneNumber
    {
        get
        {
            return this.phoneNumberValue;
        }

set
        {
            if (value != this.phoneNumberValue)
            {
                this.phoneNumberValue = value;
                NotifyPropertyChanged("PhoneNumber");
            }
        }
    }
}

转载于:https://www.cnblogs.com/zhangtao/archive/2011/04/09/2010509.html

INotifyPropertyChanged 接口相关推荐

  1. WPF学习总结1:INotifyPropertyChanged接口的作用

    在代码中经常见到这个接口,它里面有什么?它的作用是什么?它和依赖属性有什么关系? 下面就来总结回答这三个问题. 1.这个INotifyPropertyChanged接口里就一个PropertyChan ...

  2. INotifyPropertyChanged 接口 CallerMemberName属性

    调用方信息 使用调用方信息属性,可以获取关于调用方的信息传递给方法. 可以获取源代码.行号在源代码和调用方的成员名称的文件路径. 此信息用于跟踪,调试和创建诊断工具非常有用.若要获取此信息,则使用适用 ...

  3. 转载:如何优雅的实现INotifyPropertyChanged接口

    转载来源:http://www.cnblogs.com/TianFang/p/6240933.html 如何优雅的实现INotifyPropertyChanged接口 INotifyPropertyC ...

  4. [Silverlight入门系列]使用MVVM模式(3):Model的INotifyPropertyChanged接口实现

    当客户端绑定一个数据模型以后,数据模型变化以后可以自动通知客户端更新界面显示,这就是INotifyPropertyChanged接口要做的工作.INotifyPropertyChanged 接口用于向 ...

  5. [Silverlight入门系列]使用MVVM模式(7):ViewModel的INotifyPropertyChanged接口实现

    本文说说ViewModel的这个INotifyPropertyChanged接口可以用来做啥? 举例1:我有个TabControl,里面放了很多View,每个由ViewModel控制,我想是想TabS ...

  6. Silverlight中OneTime,OneWay,TwoWay及INotifyPropertyChanged 接口的理解

    今天有时间把Silverlight中OneTime,OneWay,TwoWay及INotifyPropertyChanged 接口的理解等数据绑定方面的东西理解学习了下!下面是我的笔记. (一)前台代 ...

  7. C# API中的模型和它们的接口设计

    \ 关键要点 \\ 可变模型应该具备自我验证的能力,并实现验证接口.\\t 在共享对象时(特别是在跨线程共享时),考虑使用不可变模型.\\t 考虑支持MVVM风格UI的单层和多层撤消.\\t 在实现属 ...

  8. 【转载】wpf数据绑定binding与INotifyPropertyChanged

    WPF数据绑定号称是:数据变化会通过界面更新数据,这对新手而言,绝对是个误区,在我听说这句话的时候,我真是高兴,用了以后才发现其实没有那么美.要实现前面号称的特性,需要三个条件:1.进行绑定,2.绑定 ...

  9. wpf 绑定数据无法更新ui控件可能存在的问题

    BindingMode的枚举值有: ① OneWay ② TwoWay ③ OneTime:根据源端属性值设置目标属性值,之后的改变会被忽略,除非调用BindingExpression.UpdateT ...

最新文章

  1. php伪造页面url地址,php 伪造HTTP_REFERER页面URL来源的三种方法
  2. Struts(十):OGNL表达式(一)
  3. 在斜坡上哪个物体滚的最快_人教版一年级上册 第十七课 会滚的玩具
  4. 单臂路由配置命令_如何通过单臂路由实现VLAN间通信?(配置篇)
  5. .NET Core Web APi大文件分片上传研究
  6. oracle用户密码已过期,oracle用户密码过期的处理方法
  7. 高等数学基础 - 高等数学主要内容
  8. 几个北邮和交大学霸的公众号,值得学习
  9. 使用文件进行输入输出的两种方式(算法竞赛入门经典第2章)
  10. [转]SQL Server 2000执行计划成本(2/5)
  11. 飞机大战(微信小游戏)
  12. 【云计算学习教程】软件(程序)是什么?
  13. Android 开发者的下半场
  14. transformation-matrix
  15. python 公司名称 相似度分析_Python文本相似度分析
  16. PHP实现常用设计模式之观察者模式
  17. [ZZ]AppiumForWindows 菜鸟计划合集
  18. 鼎信诺虚拟服务器导数,鼎信诺审计前端取数讲解.ppt
  19. hloj#168“倒牛奶”解题讨论
  20. 【项目经验】最新最全ElasticSearch操作详解

热门文章

  1. oracle11g imp性能,怎么最快地把本机的oracle11g数据导入xe
  2. memkind版本查看_QQ 20周年来啦!扫码查看你的回忆
  3. 主从多机matlab代码,Jenkins的Master Slave主从进行多机多环境部署-配置
  4. 第四范式入围Gartner新兴技术与趋势影响力雷达全球代表厂商
  5. 决策类AI成最具商业价值应用产品 第四范式入选Gartner中国5大代表厂商
  6. 趣学python3(2)-添加以数字文字形式使用下划线的功能,以提高可读性
  7. 【机器学习】图解机器学习中的 12 种交叉验证技术
  8. 【Python】简约而不简单|值得收藏的Numpy小抄表(含主要语法、代码)
  9. 【Python基础】学习Python 一定要吃透这 5 个内置函数
  10. 【NLP】通俗讲解从Transformer到BERT模型!