关于databinding的细节

原文在此:http://www.codeproject.com/Articles/24656/A-Detailed-Data-Binding-Tutorialhtml

完整译文在此:http://www.cnblogs.com/lichence/archive/2012/02/17/2356001.html数据库

译不下去的笔记在此:编码

System.Windows.Forms.BindingSource是2.0里边的一个新类。赶脚微软想用BindingSource取代之前的CurrencyManager和BindingContext,因此本篇就只讲讲BingdingSource。code

首先:orm

  • Control.DataBindings集合持有全部Binding的对象,每一个Binding的对象都有一个属性DataSource,用来标明Object的type;  
  • ListBox/DataGridView等的DataSource,能够是Object  
  • BindingSource类,有DataSource属性。

so,DataSource究竟是干吗的?……在现实编码中,一般用BindingSource实例做为Bindings对象们的DataSource属性的值,Binddings[x].DataSource is BindingSource。htm

若是你用数据库,那么这个BindingSource.DataSource一般是DataSet,若是不用数据库,那它极可能是一个自定义类的实例。对象

使用data binding的方法千千万,最经常使用之一:给一个Control的DataSource绑一个BindingSource对象。  blog

能够认为BindingSource是一个二合一的数据源。二合一一般意味着:   事件

  1. 有一个叫Current的Object实例,Control的某属性能够绑定到这个Current对象的某属性上。   
  2. 有一个实现了IList的列表,里边全是和Current同样类型的对象。List是BindingSource的只读属性,用来返回一个内部列表(若是没设置DataMember的话),或者返回一个外部列表(若是设置了DataMember)。Current老是这个List的一员,要么是null。当设置该DataSource为一个单一的实例时,这个列表就只包含这个惟一的Object。

控件不一样数据绑定的方式也有不一样:   ip

  1. ComboBox和ListBox使用DataSource和DisplayMember绑定一个List。先将DataSource赋值为BindingSource对象,而后设置DisplayMember属性为Current的某个属性。   
  2. DataGrid和DataGridView使用DataSource属性绑定一个List。这俩控件没有DisplayMember属性。DataGridView有一个DataMember的属性,它看起来和BindingSource的DataMember很类似。若是DataSource不是BindingSource的话,就要用DataGridView.DataMember来设置数据源。若是DataSource是BindingSource,仍是得用BindingSource的DataMember。   
  3. TextBox/Button/CheckBox这类简单的控件,经过Control.DataBindings集合将自身绑定到数据源的Current对象的某一属性上。   

      * grid什么的它们的DataBindings属性,即便有东西,也是无用的。这个要注意。

再简化一下也就是两种绑定方式,一个是经过Control.DataSource=,一个是Control.DataBindings.Add()。

经常使用于绑定的属性包括:   

  1. CheckBox和RadioButton的Checked;   
  2. ComboBox/ListBox/ListView的SelectedIndex   
  3. ComboBox/ListBox的SelectedValue   
  4. 控件的Enable/Visible   
  5. 控件的Text

Tips: ListView和TreeView的内容是不能绑定的(天怒人怨),它的SelectIndex和Enable这样的属性能够绑定。(天怒人怨)

……

 •The latter handler (Binding.Target_Validate) passes the new value through a couple of internal classes, BindToObject and ReflectPropertyDescriptor, the latter of which uses Reflection to actually change the value in the Airplane and then call base.OnValueChanged in its base class, PropertyDescriptor.

这里貌似是讲到TextBox的Validate事件才传新值进行binding的同步。(相关:为何只有textbox lose focus之后,数据才会刷新??)

txtModel.DataBindings[0].DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;

默认值是OnValidated

相关文章
相关标签/搜索