通过 INotifyPropertyChanged 实现观察者模式

, );
}
].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
很好,很强大,高端大气上档次

{
// 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 实现观察者模式的更多相关文章
- 【转】通过 INotifyPropertyChanged 实现观察者模式
通过 INotifyPropertyChanged 实现观察者模式 原博客地址 http://frankdzu.blog.sohu.com/117654536.html 普通观察者模式存在的问题 我们 ...
- 观察者模式实现INotifyPropertyChanged
其实一直不知道INotifyPropertyChanged这个接口中PropertyChanged事件是什么时候有值的,因为在使用的时候,只要按步骤来就可以,因为我自己并没有对这个事件赋值,所以很好奇 ...
- C#-INotifyPropertyChanged(解决数据绑定的界面刷新问题)
最近做项目用到DataGridView,用它绑定数据源后,如果数据源中的数据修改无法及时刷新到控件上,必须切换单元格的焦点才能导致刷新显示新数值,通过查官方文档,用INotifyPropertyCha ...
- WPF学习总结1:INotifyPropertyChanged接口的作用
在代码中经常见到这个接口,它里面有什么?它的作用是什么?它和依赖属性有什么关系? 下面就来总结回答这三个问题. 1.这个INotifyPropertyChanged接口里就一个PropertyChan ...
- 23种设计模式--观察者模式-Observer Pattern
一.观察者模式的介绍 观察者模式从字面的意思上理解,肯定有两个对象一个是观察者,另外一个是被观察者,观察者模式就是当被观察者发生改变得时候发送通知给观察者,当然这个观察者可以是多个对象,在项 ...
- 谈谈JS的观察者模式(自定义事件)
呼呼...前不久参加了一个笔试,里面有一到JS编程题,当时看着题目就蒙圈...后来研究了一下,原来就是所谓的观察者模式.就记下来...^_^ 题目 [附加题] 请实现下面的自定义事件 Event 对象 ...
- ObserverPattern(观察者模式)
import java.util.ArrayList; import java.util.List; /** * 观察者模式 * @author TMAC-J * 牵一发而动全身来形容观察者模式在合适 ...
- java观察者模式
像activeMQ等消息队列中,我们经常会使用发布订阅模式,但是你有没有想过,客户端时如何及时得到订阅的主题的信息?其实就里就用到了观察者模式.在软件系统中,当一个对象的行为依赖于另一个对象的状态 ...
- Backbone源码解析(六):观察者模式应用
卤煮在大概一年前写过backbone的源码分析,里面讲的是对一些backbone框架的方法的讲解.这几天重新看了几遍backbone的源码,才发现之前对于它的理解不够深入,只关注了它的一些部分的细节和 ...
随机推荐
- requirejs 笔记
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- cmd 进入不同的驱动盘及上下级目录
“开始”=>“运行”,输入”cmd“,此时进入的是系统管理员的等待命令 如果想进入相应的盘符,如 d 盘,则输入 cd d:\,然后再次输入 d: 即可进入 d: 盘,输入两次相当于第二在是询问 ...
- css重置样式表reset.css
body, h1, h2, h3, h4, h5, h6, hr, p, blockquote,/* structural elements 结构元素 */ dl, dt, dd, ul, ol, l ...
- AS3.0声明静态属性和静态方法
静态属性的变量声明要加static,static var 名称:属性类型=值 静态属性的常量声明要加static或者const,并在声明时就要赋值.static||const var 名称:属性类型= ...
- java 转html为pdf
最近有个需求转html为pdf . 用过itext . pd4ml ,都不理想,不是样式有问题,就是页面大小有问题. 或字体有问题. 解决办法是通过wkhtmltopdf工具 , 下载地址为:htt ...
- Abstract Factory
工厂模式比较好理解,其实就是通过访问工厂返回单一的对象/多个对象.那么抽象工厂就是返回多个抽象对象.这意味工厂返回对象纵向的一个扩展.但是很多时候,抽象工厂是两个维度的扩展,比方说在数据库类型和表对象 ...
- matlab常用小函数(二)
numel 元素个数 assert 表达式为假时输出某个字符串 int2str 整形转化为字符串型 numel(A) 返回A中的元素个数,A可以是任何的数据结构,如向量.矩阵.元胞.结构体等 asse ...
- [BZOJ 3564] [SHOI2014] 信号增幅仪 【最小圆覆盖】
题目链接:BZOJ - 3564 题目分析 求最小椭圆覆盖,题目给定了椭圆的长轴与 x 轴正方向的夹角,给定了椭圆长轴与短轴的比值. 那么先将所有点旋转一个角度,使椭圆长轴与 x 轴平行,再将所有点的 ...
- [UOJ 74] 【UR #6】破解密码
题目链接:UOJ - 74 题目分析 题目中,将字符串 S 的第一个字符移到末尾,其他字符向前移动一个位置,f(S) 就从 Hi 变成了 Hi+1. 我们分析一下这个过程:假设第一个字符为 c, (H ...
- js optimization and performance
http://www.codeproject.com/Articles/551733/Walkthrough-3aplusUsingplustheplusRequireJSplusOpt http:/ ...