INotifyPropertyChanged, Interface
Data Object(class) impliment INotifyPropertyChanged; then the Object can update BindingSource.
Implimentation
public abstract class NotifyProperyChangedBase : INotifyPropertyChanged
{
#region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; #endregion #region methods protected bool CheckPropertyChanged<T>(string propertyName, ref T oldValue, ref T newValue)
{
if (oldValue == null && newValue == null)
{
return false;
} if ((oldValue == null && newValue != null) || !oldValue.Equals((T)newValue))
{
oldValue = newValue; FirePropertyChanged(propertyName); return true;
} return false;
} protected void FirePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); }
} #endregion }
Use
class NotifiablePerson : MyComponentModel.NotifyProperyChangedBase
{
private string _firstName; public string FirstName
{
get { return _firstName; }
set
{
if (this.CheckPropertyChanged<string>("FirstName", ref _firstName, ref value))
{
this.DisplayNameChanged();
}
}
} private string _lastName; public string LastName
{
get { return _lastName; }
set
{
if (this.CheckPropertyChanged<string>("LastName", ref _lastName, ref value))
{
this.DisplayNameChanged();
}
}
} public string DisplayName
{
get { return _firstName + " " + _lastName; }
} private void DisplayNameChanged()
{
this.FirePropertyChanged("DisplayName");
}
}
One Way binding => textBox event -> textBox1_TextChanged(object sender, EventArgs e) update Data/Property -> Property update another control(listBox2.DataSource = nl;).
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
LoadRegularList(); LoadNotiList();
} private void LoadRegularList()
{
System.ComponentModel.BindingList<RegularPerson> rl = new System.ComponentModel.BindingList<RegularPerson>(); RegularPerson rp = rl.AddNew();
rp.FirstName = "John";
rp.LastName = "Smith"; RegularPerson rp2 = rl.AddNew();
rp2.FirstName = "Joe";
rp2.LastName = "Shmoe"; listBox1.DataSource = rl;
} private void LoadNotiList()
{
System.ComponentModel.BindingList<NotifiablePerson> nl = new System.ComponentModel.BindingList<NotifiablePerson>(); NotifiablePerson np = nl.AddNew();
np.FirstName = "Jane";
np.LastName = "Doe"; NotifiablePerson np2 = nl.AddNew();
np2.FirstName = "Mary";
np2.LastName = "Smith"; listBox2.DataSource = nl;
} private void textBox1_TextChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItem != null)
{
((RegularPerson)listBox1.SelectedItem).FirstName = textBox1.Text;
}
} private void textBox2_TextChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItem != null)
{
((RegularPerson)listBox1.SelectedItem).LastName = textBox2.Text;
}
} private void textBox3_TextChanged(object sender, EventArgs e)
{
if (listBox2.SelectedItem != null)
{
((NotifiablePerson)listBox2.SelectedItem).FirstName = textBox3.Text;
}
} private void textBox4_TextChanged(object sender, EventArgs e)
{
if (listBox2.SelectedItem != null)
{
((NotifiablePerson)listBox2.SelectedItem).LastName = textBox4.Text;
}
}
}

TextBox(First Name, Last Name) => Notifiable Person => ListBox
Add a modifying property to tag changes of an object.
Code =>
class NotifiablePerson : MyComponentModel.NotifyProperyChangedBase
{
public bool Modified {get; set;} private string _firstName; public string FirstName
{
get { return _firstName; }
set
{
if (this.CheckPropertyChanged<string>("FirstName", ref _firstName, ref value))
{
this.DisplayNameChanged();
Modified = true;
}
}
} private string _lastName; public string LastName
{
get { return _lastName; }
set
{
if (this.CheckPropertyChanged<string>("LastName", ref _lastName, ref value))
{
this.DisplayNameChanged();
Modified = true;
}
}
} public string DisplayName
{
get { return _firstName + " " + _lastName; }
} private void DisplayNameChanged()
{
this.FirePropertyChanged("DisplayName");
}
}
usage = >
private void LoadNotiList()
{
System.ComponentModel.BindingList<NotifiablePerson> nl = new System.ComponentModel.BindingList<NotifiablePerson>(); NotifiablePerson np = nl.AddNew();
np.FirstName = "Jane";
np.LastName = "Doe";
np.Modified = false; NotifiablePerson np2 = nl.AddNew();
np2.FirstName = "Mary";
np2.LastName = "Smith";
np2.Modified = false; listBox2.DataSource = nl; dataGridView1.DataSource = nl;
}
Add Event = > CellValueChanged
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Invalidate();
}


INotifyPropertyChanged, Interface的更多相关文章
- (WPF) 基本题
What is WPF? WPF (Windows Presentation foundation) is a graphical subsystem for displaying user inte ...
- Optimizing Performance: Data Binding(zz)
Optimizing Performance: Data Binding .NET Framework 4.5 Other Versions Windows Presentation Founda ...
- Local Database Sample Model
[Table] public class AddTableNameHere : INotifyPropertyChanged, INotifyPropertyChanging { // // TODO ...
- Conway's Game of Life: An Exercise in WPF, MVVM and C#
This blog post was written for the Lockheed Martin Insight blog, sharing here for the external audie ...
- 如何引用XML文件生成C#类
目录 XSD File Generate Class File Simply. 1 Why use XSD file to create C# classes?... 2 How to convert ...
- (WPF) 再议binding:点击User Control时,User Control变换颜色或做其他的处理。
Binding 是前台UI(显示层)和后台代码(数据层)的桥梁.理论上当后台的数据变动时,显示的数据或样式应该随之而变.这些是动态的. 对于Binding的设置可以在前台Xaml,也可以在后台Code ...
- WPF/MVVM 快速开发
http://www.codeproject.com/Articles/165368/WPF-MVVM-Quick-Start-Tutorial 这篇文章醍醐灌顶,入门良药啊! Introductio ...
- 『WPF』DataGrid的使用
原文 『WPF』DataGrid的使用 几点说明 这里主要是参考了MSDN中关于DataGrid的说明 这里只会简单说明在WPF中,DataGird最简单的使用方法 对于MSDN中的翻译不会很详细,也 ...
- [No000012E]WPF(6/7):概念绑定
WPF 的体系结构,标记扩展,依赖属性,逻辑树/可视化树,布局,转换等.今天,我们将讨论 WPF 最重要的一部分——绑定.WPF 带来了优秀的数据绑定方式,可以让我们绑定数据对象,这样每次对象发生更改 ...
随机推荐
- C#高级编程笔记 Day 4, 2016年9月 12日(接口)
1.定义和实现接口:接口名称通常上以字母 I 开头 例子:定义IBankAccount接口 namespace Test.YinXi{ public interface IBankAccount{ v ...
- PHP PDO的FETCH_NUM、FETCH_BOTH、FETCH_ASSOC
不容易混淆的有:FETCH_CLASS,FETCH_COLUMN,FETCH_OBJ... 数据库的连接准备部分 $dsn = 'mysql:host=127.0.0.1;port=3306;dbna ...
- Linux压缩和解压汇总
各种后缀的压缩包压缩方法和解压方法 压缩包 压缩 解压 .tar.gz和.tgz tar -czf jpg.tar.gz *.jpg tar -xzvf filename -C path .tar.b ...
- 两个已排序数组进行合并后的第K大的值--进军硅谷
我看到此题时,首先想到一个一个比较遍历过去,这是最暴力的方法,后面我想到了已经排序,那么对每个数组进行二分,然后比较这两个值.此书第三种解法,挺不错,只对那个长度较小的数组进行二分查找,保证i+j-1 ...
- 基于Struts2CRUD的质量属性
基于struts2框架开发的<学生管理系统>的质量属性 我们经常重新设计系统,可能不是因为该系统在功能上有缺陷,而是由于:系统运行速度太慢.系统容易受到外界攻击.用另外的一句话说:我们修改 ...
- Jmeter连接Mysql
1.下载连接mysql数据库jar包,地址:http://files.cnblogs.com/files/xiaoxitest/mysql-connector-java-5.1.28.zip(因不支持 ...
- thinkphp中volist标签
volist标签 volist标签主要用于在模板中循环输出数据集或者多维数组 volist(name,id,offset,length,key,mod,empty) name(必须):要输出的数据模型 ...
- Angular.element和$document的使用方法分析,代替jquery
AngularJs是不直接操作DOM的,但是在平时的开发当中,我们有的时候还是需要操作一些DOM的,如果使用原生的JS的话操作过于麻烦,所以大家一般都是使用jQuery,jQuery虽然好用,但是An ...
- 使用Lua脚本语言开发出高扩展性的系统,AgileEAS.NET SOA中间件Lua脚本引擎介绍
一.前言 AgileEAS.NET SOA 中间件平台是一款基于基于敏捷并行开发思想和Microsoft .Net构件(组件)开发技术而构建的一个快速开发应用平台.用于帮助中小型软件企业建立一条适合市 ...
- 【转】Java Web 项目获取运行时路径 classpath
Java Web 项目获取运行时路径 classpath 假设资源文件放在maven工程的 src/main/resources 资源文件夹下,源码文件放在 src/main/java/下, 那么ja ...