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 带来了优秀的数据绑定方式,可以让我们绑定数据对象,这样每次对象发生更改 ...
随机推荐
- what's cloud computing? IaaS
Cloud computing has changed the ITC industry. Companies like Amazon, Google and Microsoft have built ...
- PHPStorm XDebug的安装
环境: 我的系统: 4.4.0-43-generic #63-Ubuntu SMP Wed Oct 12 13:48:03 UTC 2016 x86_64 x86_64 x86_64 GNU/Linu ...
- C++中的 :: 用法
::是运算符中等级最高的,它分为三种:1)global scope(全局作用域符),用法(::name)2)class scope(类作用域符),用法(class::name)3)namespace ...
- 开发基于Edge渲染内核的浏览器应用
在使用Edge之前,我们先来看看UWP(Universal Windows Platform)平台.微软研发了多种设备平板.手机.Xbox.个人电脑等,在此之前,如果需要给每台设备开发程序,都需要对应 ...
- Python网络编程之线程,进程
一. 线程: 基本使用 线程锁 线程池 队列(生产者消费者模型) 二. 进程: 基本使用 进程锁 进程池 进程数据共享 三. 协程: gevent greenlet 四. 缓存: memcache ...
- 两个已排序数组进行合并后的第K大的值--进军硅谷
我看到此题时,首先想到一个一个比较遍历过去,这是最暴力的方法,后面我想到了已经排序,那么对每个数组进行二分,然后比较这两个值.此书第三种解法,挺不错,只对那个长度较小的数组进行二分查找,保证i+j-1 ...
- Visual Studio 2013 智能提示
Visual Studio 2013中,智能提示功能突然用不了,查了一下,使用命令行重置VS的方法解决了这个问题.步骤如下: 开始菜单 -->所有程序-->Visual Studio 20 ...
- AJAX里,使用XML返回数据类型,实现简单下拉列表
XML:可扩展标记语言 HTML:超文本标记语言 标签:<标签名></标签名> 特点: 1.必须要有一个根 2.标签名自定义 3.对大小写敏感 4.有开始就要有结束 5.同一级 ...
- 使用vs2008搭建php扩展环境
所需要的工具或者文件: 1.php源码,去官网下载即可.http://windows.php.net/download 2.php-sdk:php 开发工具包 3.deps类库 4.Cygwin:wi ...
- 会话控制:Cookie和session
HTTP(超文本传输协议)定义了通过万维网(WWW)传输文本.图形.视频和所有其他数据所有的规则.HTTP是一种无状态的协议,说明每次请求的处理都与之前或之后的请求无关.虽然这种简化实现对于HTTP的 ...