DataBindings属性是很多控件都有的属性,作用有2方面。一方面是用于与数据库的数据进行绑定,进行数据显示。另一方面用于与控件或类的对象进行数据绑定。这里主要关注后者。主要用法是将某个对象的某个属性与指定对象的指定属性进行关联.
Label、TextBox等都包含DataBindings属性,其类型为ControlBindingsCollection,是Binding类的集合。Binding类代表某对象属性值和某控件属性值之间的简单绑定。如可以将TextBox的Text属性值绑定到Label的Text属性值,这样,当TextBox中的文本被修改的时候,Label的文本也会及时进行修改,如下面的代码所示:
1
|
Label1.DataBindings.Add( "Text" ,TextBox1, "Text" ); |
Binding类除了可以将对象的属性绑定到控件的属性之外,还可以将对象列表中当前对象的属性值绑定到控件的属性。
当使用Binding的构造函数创建实例时,必须指定三项内容:
- 要绑定到的控件属性的名称
- 数据源
- 数据源中解析为列表或属性的导航路径
其中,数据源可以为:
- 实现 IBindingList 或 ITypedList 的任何类。包括:DataSet、DataTable、DataView 或 DataViewManager。
- 实现 IList 的任意索引集合类。(必须在创建 Binding 之前创建和填充该集合,并且列表中的所有对象必须为同一类型,否则将引发异常)
- 强类型对象的强类型 IList。
导航路径可以为空字符串(默认将调用数据源的ToString()方法)、单个属性名称或用点分隔的名称层次结构。
名称层次结构是什么意思呢?比如我们有一个Company类,它包含Name属性和Employees属性(公司所有Employee的集合),而Employee类又包含Name属性。那么,如果要将Company的Name属性绑定到TextBox控件的Text属性,代码为:
1
|
TextBox1.DataBindings.Add( "Text" , company, "Name" ); |
如果要绑定Employees的Name属性,代码为:
1
|
TextBox1.DataBindings.Add( "Text" , company, "Employees.Name" ); |
Employess.Name即为用点分隔的名称层次结构。在这里,Employees为一个集合,将Employees.Name绑定到TextBox会出现什么情况呢?测试后可知,TextBox将显示Employees集合中第一个Employee的Name属性。
示例:
界面
代码实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace DataBindingsDemo { public partial class FrmDataBindings : Form { public FrmDataBindings() { InitializeComponent(); } private void FrmDataBindings_Load( object sender, EventArgs e) { //绑定到DataTable DataTable dtSource = GetDataTable(); this .textBox1.DataBindings.Add( "Text" , dtSource, "StudentNo" ); this .textBox2.DataBindings.Add( "Text" , dtSource, "StudentName" ); this .textBox3.DataBindings.Add( "Text" , dtSource, "Sex" ); //绑定到实体对象 Student stu = new Student() { StudentNo=2,StudentName= "测试2" ,Sex= "女" }; //必须是绑定到对象的属性(此例中绑定到StudentNo,而不是student), this .textBox4.DataBindings.Add( "Text" , stu, "StudentNo" ); this .textBox5.DataBindings.Add( "Text" , stu, "StudentName" ); this .textBox6.DataBindings.Add( "Text" , stu, "Sex" ); } private DataTable GetDataTable() { DataTable dt = new DataTable(); DataColumn dcNo = new DataColumn( "StudentNo" , typeof (Int32)); DataColumn dcName = new DataColumn( "StudentName" , typeof ( string )); DataColumn dcSex = new DataColumn( "Sex" , typeof ( string )); dt.Columns.Add(dcNo); dt.Columns.Add(dcName); dt.Columns.Add(dcSex); dt.Rows.Add( new object [] { 1, "测试" , "男" }); return dt; } } public class Student { private int studentNo; public int StudentNo { get { return studentNo; } set { studentNo = value; } } private string studentName; public string StudentName { get { return studentName; } set { studentName = value; } } private string sex; public string Sex { get { return sex; } set { sex = value; } } } } |
运行效果:
到此这篇关于Winform项目中TextBox控件DataBindings属性的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/dotnet261010/p/6731126.html