INotifyPropertyChanged
它的作用:向客户端发出某一属性值已更改的通知。
当属性改变时,它可以通知客户端,并进行界面数据更新.而我们不用写很多复杂的代码来更新界面数据,这样可以做到方法简洁而清晰,松耦合和让方法变得更通用.可用的地方太多了:例如上传进度,实时后台数据变更等地方。
它的作用:向客户端发出某一属性值已更改的通知。
当属性改变时,它可以通知客户端,并进行界面数据更新.而我们不用写很多复杂的代码来更新界面数据,这样可以做到方法简洁而清晰,松耦合和让方法变得更通用.可用的地方太多了:例如上传进度,实时后台数据变更等地方.目前我发现winform和silverlight都支持,确实是一个强大的接口.
在构造函数中先绑定

, );
        }

].CustomerName = ].PhoneNumber = , );
        }

].CustomerName = ].PhoneNumber = "(708)555-0150";

//如果数据源是换成List<T>只有刷新以后才能即使更新。
            this.customersDataGridView.Refresh();
        }

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

}

}

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

(3)、让INotifyPropertyChanged的实现更优雅一些http://tech.ddvip.com/2009-05/1242645380119734_2.html

很好,很强大,高端大气上档次

public class DemoCustomer1 : PropertyChangedBase
    {
        // These fields hold the values for the public properties.
        private Guid idValue = Guid.NewGuid();
        private string customerNameValue = String.Empty;
        private string phoneNumberValue = String.Empty;

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

// This is the public factory method.
        public static DemoCustomer1 CreateNewCustomer()
        {
            return new DemoCustomer1();
        }

// 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;
                    this.NotifyPropertyChanged(p => p.CustomerName);
                    //NotifyPropertyChanged("CustomerName");
                }
            }
        }

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

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

/// <summary>
    /// 基类
    /// </summary>
    public class PropertyChangedBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

public void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

/// <summary>
    /// 扩展方法
    /// </summary>
    public static class PropertyChangedBaseEx
    {
        public static void NotifyPropertyChanged<T, TProperty>(this T propertyChangedBase, Expression<Func<T, TProperty>> expression) where T : PropertyChangedBase
        {
            var memberExpression = expression.Body as MemberExpression;
            if (memberExpression != null)
            {
                string propertyName = memberExpression.Member.Name;
                propertyChangedBase.NotifyPropertyChanged(propertyName);
            }
            else
                throw new NotImplementedException();
        }
    }View Code

通过 INotifyPropertyChanged 实现观察者模式的更多相关文章

  1. 【转】通过 INotifyPropertyChanged 实现观察者模式

    通过 INotifyPropertyChanged 实现观察者模式 原博客地址 http://frankdzu.blog.sohu.com/117654536.html 普通观察者模式存在的问题 我们 ...

  2. 观察者模式实现INotifyPropertyChanged

    其实一直不知道INotifyPropertyChanged这个接口中PropertyChanged事件是什么时候有值的,因为在使用的时候,只要按步骤来就可以,因为我自己并没有对这个事件赋值,所以很好奇 ...

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

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

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

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

  5. 23种设计模式--观察者模式-Observer Pattern

    一.观察者模式的介绍      观察者模式从字面的意思上理解,肯定有两个对象一个是观察者,另外一个是被观察者,观察者模式就是当被观察者发生改变得时候发送通知给观察者,当然这个观察者可以是多个对象,在项 ...

  6. 谈谈JS的观察者模式(自定义事件)

    呼呼...前不久参加了一个笔试,里面有一到JS编程题,当时看着题目就蒙圈...后来研究了一下,原来就是所谓的观察者模式.就记下来...^_^ 题目 [附加题] 请实现下面的自定义事件 Event 对象 ...

  7. ObserverPattern(观察者模式)

    import java.util.ArrayList; import java.util.List; /** * 观察者模式 * @author TMAC-J * 牵一发而动全身来形容观察者模式在合适 ...

  8. java观察者模式

      像activeMQ等消息队列中,我们经常会使用发布订阅模式,但是你有没有想过,客户端时如何及时得到订阅的主题的信息?其实就里就用到了观察者模式.在软件系统中,当一个对象的行为依赖于另一个对象的状态 ...

  9. Backbone源码解析(六):观察者模式应用

    卤煮在大概一年前写过backbone的源码分析,里面讲的是对一些backbone框架的方法的讲解.这几天重新看了几遍backbone的源码,才发现之前对于它的理解不够深入,只关注了它的一些部分的细节和 ...

随机推荐

  1. Setup FTP Server On CentOS, RHEL, Scientific Linux 6.5/6.4/6.3

    setsebool allow_ftpd_full_access onsetsebool -P ftp_home_dir on vsftpd (Very Secure File Transport P ...

  2. JavaScript Ajax + Promise

    AJAX 在现代浏览器上写AJAX主要依靠XMLHttpRequest对象: function success(text) { var textarea = document.getElementBy ...

  3. sec:authorize 标签 通过不通过权限例子

    1. 方式一     <sec:authorize ifAnyGranted="ROLE_A">     <a href="a.jsp"> ...

  4. 《JavaScript高级程序设计 第3版》-学习笔记-3

    P84-P137页, 这一章看的真久,这个月事太多了.有些内容在代码注释里没提出来了 1.JS强大的数组类型,元素类型任意,提供了非常多的操作数组的方法和属性 /* 数组类型 */ //stack v ...

  5. Java面试题相关内容

    选择题(共5题,每题1.5分,共75分.多选题选不全或选错都不得分.)1. 以下属于面向对象的特征的是(C,D).(两项)A) 重载B) 重写C) 封装D) 继承 2. 以下代码运行输出是(C)pub ...

  6. applicationContext.xml详解 spring+mybatis+struts

    今天给大家详细解释一项关于Spring的applicationContext.xml文件,这对于初学者来说,应该是很有帮助的, 以下是详解Spring的applicationContext.xml文件 ...

  7. 关于执行ST_Geometry的st_centroid函数时报ORA-28579错误的问题

    环境 SDE版本:10./10.2/10.2.1/10.2.2 Oracle版本:11g R2 11.2.0.1 Windows版本:Windows Server 2008 R2 问题描述及原因 以全 ...

  8. 变更到Android4.4的问题

    更新到Android 4.4,写了个小程序.发现运行不起来了.抛空指针异常.debug模式下,发现在onCreate方法中获取Button是null. Android 4.4把layout进行了重组, ...

  9. Contest20140710 eagleeggs

    eagleeggs|eagleeggs.in|eagleeggs.out 题目描述: 共有N个硬度相同的鹰蛋,硬度是一个整数(并且已知其不大于H),表示这个蛋从天上掉下来不摔碎的最大高度.为了找出这个 ...

  10. bzoj 1059: [ZJOI2007]矩阵游戏 二分图匹配

    1059: [ZJOI2007]矩阵游戏 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1891  Solved: 919[Submit][Statu ...