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 带来了优秀的数据绑定方式,可以让我们绑定数据对象,这样每次对象发生更改 ...
随机推荐
- 1.从Node.js链接到MongoDB
MongoDB采用了MongoDB Node.js驱动程序作为标准. 1.安装MongoDB驱动 npm install mongoDB npm install mongoose require('m ...
- getComputedStyle的简单用法
var number=window.getComputedStyle("元素").style样式名
- Python 中的@修饰符作用
在Python 2.4以上的的函数中偶尔会看到函数定义的上一行有@functionName的修饰,这一下这个语法细节,其实这有点像C语言带参数的宏操作,解释器读到这样的修饰之后,会先解析@后的内容,直 ...
- 源码编译安装mysql
1 概述 首先来看下mysql的下载地址: http://ftp.plusline.de/mysql/Downloads/ 这里有mysql的各种版本 操作系统:CentOS releas ...
- redis-3.2.5 make 报错
make[]: Entering directory `/usr/local/src/redis-/src' CC adlist.o In file included : zmalloc.h::: e ...
- Oracle安装介质及补丁集下载地址
Oracle9i Database Release 2 Enterprise/Standard/Personal Edition for Windows NT/2000/XP http://downl ...
- windows查看端口占用情况
1,查看指定端口被哪个进程占用. >netstat -ano|findstr 8008 TCP 127.0.0.1:8083 0.0.0.0:0 ...
- mysql导入导出,及错误记录
进入mysql的bin目录,如果mysql的bin添加了环境变量则不用. 导出,不指定编码则默认为:utf8mb4.: mysqldump -u root -h 127.0.0.1 -P 3307 - ...
- BZOJ 3105 [CQOI2013]新Nim游戏 ——线性基
[题目分析] 神奇的题目,两人都可以第一次取走足够多堆的石子. nim游戏的规则是,如果异或和为0,那么就先手必输,否则先手有必胜策略. 所以只需要剩下一群异或和为0就可以了. 先排序,线性基扫一遍即 ...
- 最详细易懂的CRC-16校验原理(附源程序)
from:http://www.openhw.org/chudonganjin/blog/12-08/230184_515e6.html 最详细易懂的CRC-16校验原理(附源程序) 1.循环校验码( ...