上一篇中,我介绍了大致介绍了一下数据绑定实现的原理和见识了一下简单绑定。继续介绍,简单绑定还没完全说清楚。Binding对象是简单绑定中比较重要的一个类。Binding创建和维护某控件的属性与某对象的属性或对象列表中当前对象的属性之间的简单绑定。这是MSDN上的描述。上一篇中你们认识到了,绑定其实就是控件属性和对象数据之间的关联。在MSDN的描述中也说明了这一点。
我们可以建立一个Binding对象来表示这个关联,一下的代码你们已经见到过了.
Binding binding = new Binding("Text", person, "FirstName");
this.txtFirstname1.DataBindings.Add(binding);
我已经大概解释了Binding的构造函数中每个参数的含义。 现在具体来说明一下。
第一参数:你要指定一个你控件的属性(如:Text, Background, ForeColor等).
第二参数:你要指定任意一个类的实例最为数据源。可能的数据源包括:
任意对象和一下列举的对象。
说明
|
C# 示例
|
实现 IBindingList 或 ITypedList 的任何类。包括:DataSet、DataTable、DataView 或 DataViewManager。
|
DataSet ds = new DataSet("myDataSet");
|
实现 IList 以创建对象索引集合的任意类。必须在创建 Binding 之前创建和填充该集合。列表中的所有对象必须为同一类型;否则将引发异常。
|
ArrayList ar1 = new ArrayList;
Customer1 cust1 = new Customer("Louis");
ar1.Add(cust1);
|
强类型对象的强类型 IList。
|
Customer [] custList = new Customer[3];
|
以上是MSDN中列举的。
第三参数:需要指定的绑定表达式。(在前一文章中的例子中就是一个简单的属性的名称)。但并不是总是这样。我们可以考虑这样的一个例子。
比如我们又一个Employee类并且它拥有个一属性是City(表示所在的城市),而这个属性是一个City类型。City类中有一个属性是Name表示城市的名称。
在这种情况中。如果你想把Employee类的某个实例的City属性的城市名称绑定到一个TextBox中的话。我们就需要这样做:
1
//其中employee表示Employee类的实例,而“City.Name”表示控件现在绑定的是employee的City属性的Name属性的值。
2
this.textBox1.DataBindings.Add("Text",employee, "City.Name");
下面我建立一个例子来说更多的关于数据绑定表达式的用法:(下面的例子只会讲解简单绑定中涉及到的内容)
1.建立City类
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
namespace SimplyDataBinding2
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
7
public class City
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
private string _postalcode;
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
11
/// 获取或设置邮政编码。
12
/// </summary>
13
public string Postalcode
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _postalcode; }
16![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _postalcode = value; }
17
}
18
private string _name;
19![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
20
/// 获取或设置城市名称。
21
/// </summary>
22
public string Name
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
24![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _name; }
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _name = value; }
26
}
27![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public City()
{ }
28
public City(string postalcode, string name)
29![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
30
this.Postalcode = postalcode;
31
this.Name = name;
32
}
33
}
34
}
35![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2.建立Employee类
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
namespace SimplyDataBinding2
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
7
public class Employee
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
private string _name;
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
11
/// 获取或设置姓名。
12
/// </summary>
13
public string Name
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _name; }
16![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _name = value; }
17
}
18
private string _title;
19![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
20
/// 获取或设置称呼。
21
/// </summary>
22
public string Title
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
24![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _title; }
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _title = value; }
26
}
27
private City _city;
28![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
29
/// 获取或设置所在城市。
30
/// </summary>
31
public City City
32![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
33![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _city; }
34![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _city = value; }
35
}
36![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Employee()
{ }
37
public Employee(string name, string title, City city)
38![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
39
this.Name = name;
40
this.Title = title;
41
this.City = city;
42
}
43
}
44
}
45![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
请注意31行,我声明了一个City属性,它是一个City类型。
Form1.CS
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Text;
7
using System.Windows.Forms;
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
namespace SimplyDataBinding2
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
11
public partial class Form1 : Form
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
public Form1()
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15
InitializeComponent();
16
Employee employee = new Employee("Caipeng", "Mr", new City("430071", "Wuhan"));
17
this.textBox1.DataBindings.Add("Text", employee, "Name", true);
18
this.textBox2.DataBindings.Add("Text", employee, "Title", true);
19
//注意这里的数据绑定表达式。City属性本身是指向City类的一个实例的。所以它没有办法被解析为能
20
//直接显示在Text中的数据。一下的表达式所要表达的是我需要那个City实例的Name属性。
21
this.textBox3.DataBindings.Add("Text", employee, "City.Name", true);
22
}
23
}
24
}
我已经在代码做了说明。你可以自己看看。上面我重点的简介了一下数据绑定表达是的作用。接下来我们去更深入的认识一下Binding的作用。
如果你们留意了的话,你会发现你如果你的属性即使不是字符串类型一样可以很好的绑定到TextBox的Text。数据绑定在这里又发功,它负责将类型进行了转换。不过只有一些基本类型能够进行转换。如果你需要将二进制数据转换成Image类型,那你就的处理Binding类中的两个事件。Format和
Parse事件。我们通常使用这两个事件来进行数据的转换。
Format 事件:当数据被绑定到控件的时候触发。就是在数据要显示到控件中的时候。
Parse 事件:当控件的属性发生改变而导致要更新数据源的时候触发。
这两个事件是相对的,分别管理这从数据源到控件和控件到数据源的两个过程的数据格式转换的问题。
看例子:
我把上面的例子做个修改:
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Text;
7
using System.Windows.Forms;
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
namespace SimplyDataBinding2
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
11
public partial class Form2 : Form
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
Employee employee;
14
public Form2()
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
16
InitializeComponent();
17
employee = new Employee("Caipeng", "Mr", new City("430071", "Wuhan"));
18
this.textBox1.DataBindings.Add("Text", employee, "Name", true);
19
this.textBox2.DataBindings.Add("Text", employee, "Title", true);
20
//注意这里的数据绑定表达式是"City"。它是可以能够直接显示在Text属性但绝对不是你想要的值。
21
Binding cityBinding = new Binding("Text", employee, "City");
22
//你可以屏蔽一下的两句话来看看Text中到底会显示什么。
23
cityBinding.Format += new ConvertEventHandler(cityBinding_Format);
24
cityBinding.Parse += new ConvertEventHandler(cityBinding_Parse);
25
this.textBox3.DataBindings.Add(cityBinding);
26
}
27![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
string tempPostalcode = null;
29![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
30
/// 当控件更新数据源的时候触发。
31
/// </summary>
32
/// <param name="sender"></param>
33
/// <param name="e"></param>
34
void cityBinding_Parse(object sender, ConvertEventArgs e)
35![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
36
//e.value包含了控件中相应属性的值。
37
City city = new City(tempPostalcode, e.Value.ToString());
38
e.Value = city;
39
}
40
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
42
/// 当数据被绑定到控件上的时候触发。
43
/// </summary>
44
/// <param name="sender"></param>
45
/// <param name="e"></param>
46
void cityBinding_Format(object sender, ConvertEventArgs e)
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
48
//我将City类型转换成我想要的城市信息。
49
//e.Value 包含的是数据源中相应属性的值。这里是一个City类型的实例。
50
City temp = (City)e.Value;
51
tempPostalcode = temp.Postalcode;
52
e.Value = temp.Name;
53
}
54![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
private void button1_Click(object sender, EventArgs e)
56![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
57
//利用它来查看转换的结果是否正确。
58
MessageBox.Show(employee.City.Postalcode + " " + employee.City.Name);
59
}
60
}
61
}
从表现上看,和第一个例子没有什么区别,但实际实现的方式是不同的。请注意第21行和第一个例子中的21行是不同。
第一个例子
this.textBox3.DataBindings.Add("Text", employee, "City.Name", true);
第二个例子
Binding cityBinding = new Binding("Text", employee, "City");
//你可以屏蔽一下的两句话来看看Text中到底会显示什么。
cityBinding.Format += new ConvertEventHandler(cityBinding_Format);
cityBinding.Parse += new ConvertEventHandler(cityBinding_Parse);
this.textBox3.DataBindings.Add(cityBinding);
各位应该可以看的出区别了。
代码下载
完成。。。。