zoukankan      html  css  js  c++  java
  • 关于databinding的细节

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

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

    译不下去的笔记在此:

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

    首先:

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

    so,DataSource到底是干嘛的?……在现实编码中,通常用BindingSource实例作为Bindings对象们的DataSource属性的值,Binddings[x].DataSource is BindingSource。

    如果你用数据库,那么这个BindingSource.DataSource通常是DataSet,如果不用数据库,那它很可能是一个自定义类的实例。

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

    可以认为BindingSource是一个二合一的数据源。二合一通常意味着:   

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

    控件不同数据绑定的方式也有不同:   

    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

  • 相关阅读:
    Crontab '2>&1 &' 含义
    form提交方式Methor
    oracle基本术语
    在工作中常用的sql语句
    常用的删除大数据方法(游标+分段)
    oracle9i、10g、11g区别
    SSH面试总结(Hibernage面试)
    实习生招聘笔试
    TopCoder上一道600分滴题
    Oracle数据库面试题汇总
  • 原文地址:https://www.cnblogs.com/mumuliang/p/3964530.html
Copyright © 2011-2022 走看看