欢迎投稿

今日深度:

一句代码实现批量数据绑定 下(1)(3)

二、Control/DataSource映射的表示:BindingMapping

不论是数据绑定实体=〉控件),还是数据捕捉控件=〉实体)的实现都建立在两种之间存在着某种约定的映射之上,这个映射是整个DataBinder的核心所在。在这里,我定义了如下一个BindingMapping类型表示这个映射关系。

  1. public class BindingMapping: ICloneable  
  2. {  
  3.     public Type DataSourceType { get; private set; }  
  4.     public Control Control { get; set; }  
  5.     public string ControlValueProperty { get; set; }  
  6.     public string DataSourceProperty { get; set; }  
  7.     public bool AutomaticBind { get; set; }  
  8.     public bool AutomaticUpdate { get; set; }  
  9.     public string FormatString { get; set; }  
  10.     public Type ControlValuePropertyType  
  11.     {  
  12.         get { return PropertyAccessor.GetPropertyType(this.Control.GetType(), this.ControlValueProperty); }  
  13.     }  
  14.     public Type DataSourcePropertyType  
  15.     {  
  16.         get { return PropertyAccessor.GetPropertyType(this.DataSourceType, this.DataSourceProperty); }  
  17.     }  
  18.      
  19.     public BindingMapping(Type dataSourceType, Control control, string controlValueProperty, string dataSourceProperty)  
  20.     {  
  21.          //...  
  22.         this.DataSourceType         = dataSourceType;  
  23.         this.Control                = control;  
  24.         this.ControlValueProperty   = controlValueProperty;  
  25.         this.DataSourceProperty     = dataSourceProperty;  
  26.         this.AutomaticBind          = true;  
  27.         this.AutomaticUpdate        = true;  
  28.     }  
  29.     object ICloneable.Clone()  
  30.     {  
  31.         return this.Clone();  
  32.     }  
  33.     public BindingMapping Clone()  
  34.     {  
  35.         var bindingMapping = new BindingMapping(this.DataSourceType, this.Control, this.ControlValueProperty, this.DataSourceProperty);  
  36.         bindingMapping.AutomaticBind = this.AutomaticBind;  
  37.         bindingMapping.AutomaticUpdate = this.AutomaticBind;  
  38.         return bindingMapping;  
  39.     }  
  40. }  

这里我主要介绍一下各个属性的含义:

DataSourceType:作为数据源实体的类型;

Control:需要绑定的控件;

ControlValueProperty:数据需要绑定到控件属性的名称,比如TextBox是Text属性,而RadioButtonList则是SelectedValue属性;

DataSourceProperty:实体类型中的数据属性名称

AutomaticBind:是否需要进行自动绑定,通过它阻止不必要的自动数据绑定行为。默认值为True,如果改成False,基于该条映射的绑定将被忽略;

AutomaticUpdate:是否需要进行自动更新到数据实体中,通过它阻止不必要的自动数据捕捉行为。默认值为True,如果改成False,基于该条映射的数据捕捉定将被忽略;

FormatString:格式化字符串;

ControlValuePropertyType:控件绑定属性的类型,比如TextBox的绑定属性为Text,那么ControlValuePropertyType为System.String;

DataSourcePropertyType:实体属性类型。

需要补充一点的是:ControlValuePropertyType和DataSourcePropertyType使用到了之前定义的用于操作操作属性的组件ProcessAccessor。BindingMapping采用了克隆模式。


www.htsjk.Com true http://www.htsjk.com/shujukujc/18883.html NewsArticle 二、Control/DataSource映射的表示:BindingMapping 不论是数据绑定实体=〉控件),还是数据捕捉控件=〉实体)的实现都建立在两种之间存在着某种约定的映射之...
评论暂时关闭