BindingContext 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
管理从BindingManagerBase类继承的任何对象的对象的集合Control。
public ref class BindingContext : System::Collections::ICollection
public class BindingContext : System.Collections.ICollection
type BindingContext = class
interface ICollection
interface IEnumerable
Public Class BindingContext
Implements ICollection
- 继承
-
BindingContext
- 实现
示例
下面的代码示例创建四Binding个对象,用于将五个控件(一个和四TextBox个DateTimePicker控件)绑定到多个数据源。 BindingContext然后,使用该数据源获取BindingManagerBase每个数据源。
void BindControls()
{
/* Create two Binding objects for the first two TextBox
controls. The data-bound property for both controls
is the Text property. The data source is a DataSet
(ds). The data member is a navigation path in the form:
"TableName.ColumnName". */
text1->DataBindings->Add( gcnew Binding( "Text",ds,"customers.custName" ) );
text2->DataBindings->Add( gcnew Binding( "Text",ds,"customers.custID" ) );
/* Bind the DateTimePicker control by adding a new Binding.
The data member of the DateTimePicker is a navigation path:
TableName.RelationName.ColumnName string. */
DateTimePicker1->DataBindings->Add( gcnew Binding( "Value",ds,"customers.CustToOrders.OrderDate" ) );
/* Add event delegates for the Parse and Format events to a
new Binding object, and add the object to the third
TextBox control's BindingsCollection. The delegates
must be added before adding the Binding to the
collection; otherwise, no formatting occurs until
the Current object of the BindingManagerBase for
the data source changes. */
Binding^ b = gcnew Binding( "Text",ds,"customers.custToOrders.OrderAmount" );
b->Parse += gcnew ConvertEventHandler( this, &Form1::CurrencyStringToDecimal );
b->Format += gcnew ConvertEventHandler( this, &Form1::DecimalToCurrencyString );
text3->DataBindings->Add( b );
// Get the BindingManagerBase for the Customers table.
bmCustomers = this->BindingContext[ ds,"Customers" ];
/* Get the BindingManagerBase for the Orders table using the
RelationName. */
bmOrders = this->BindingContext[ds, "customers.CustToOrders"];
/* Bind the fourth TextBox control's Text property to the
third control's Text property. */
text4->DataBindings->Add( "Text", text3, "Text" );
}
protected void BindControls()
{
/* Create two Binding objects for the first two TextBox
controls. The data-bound property for both controls
is the Text property. The data source is a DataSet
(ds). The data member is a navigation path in the form:
"TableName.ColumnName". */
text1.DataBindings.Add(new Binding
("Text", ds, "customers.custName"));
text2.DataBindings.Add(new Binding
("Text", ds, "customers.custID"));
/* Bind the DateTimePicker control by adding a new Binding.
The data member of the DateTimePicker is a navigation path:
TableName.RelationName.ColumnName string. */
DateTimePicker1.DataBindings.Add(new
Binding("Value", ds, "customers.CustToOrders.OrderDate"));
/* Add event delegates for the Parse and Format events to a
new Binding object, and add the object to the third
TextBox control's BindingsCollection. The delegates
must be added before adding the Binding to the
collection; otherwise, no formatting occurs until
the Current object of the BindingManagerBase for
the data source changes. */
Binding b = new Binding
("Text", ds, "customers.custToOrders.OrderAmount");
b.Parse+=new ConvertEventHandler(CurrencyStringToDecimal);
b.Format+=new ConvertEventHandler(DecimalToCurrencyString);
text3.DataBindings.Add(b);
// Get the BindingManagerBase for the Customers table.
bmCustomers = this.BindingContext [ds, "Customers"];
/* Get the BindingManagerBase for the Orders table using the
RelationName. */
bmOrders = this.BindingContext[ds, "customers.CustToOrders"];
/* Bind the fourth TextBox control's Text property to the
third control's Text property. */
text4.DataBindings.Add("Text", text3, "Text");
}
Protected Sub BindControls()
' Create two Binding objects for the first two TextBox
' controls. The data-bound property for both controls
' is the Text property. The data source is a DataSet
' (ds). The data member is the string
' "TableName.ColumnName".
text1.DataBindings.Add(New Binding _
("Text", ds, "customers.custName"))
text2.DataBindings.Add(New Binding _
("Text", ds, "customers.custID"))
' Bind the DateTimePicker control by adding a new Binding.
' The data member of the DateTimePicker is a
' TableName.RelationName.ColumnName string.
DateTimePicker1.DataBindings.Add(New Binding _
("Value", ds, "customers.CustToOrders.OrderDate"))
' Add event delegates for the Parse and Format events to a
' new Binding object, and add the object to the third
' TextBox control's BindingsCollection. The delegates
' must be added before adding the Binding to the
' collection; otherwise, no formatting occurs until
' the Current object of the BindingManagerBase for
' the data source changes.
Dim b As Binding = New Binding _
("Text", ds, "customers.custToOrders.OrderAmount")
AddHandler b.Parse, New ConvertEventHandler(AddressOf CurrencyStringToDecimal)
AddHandler b.Format, New ConvertEventHandler(AddressOf DecimalToCurrencyString)
text3.DataBindings.Add(b)
' Get the BindingManagerBase for the Customers table.
bmCustomers = Me.BindingContext(ds, "Customers")
' Get the BindingManagerBase for the Orders table using the
' RelationName.
bmOrders = Me.BindingContext(ds, "customers.CustToOrders")
' Bind the fourth TextBox control's Text property to the
' third control's Text property.
text4.DataBindings.Add("Text", text3, "Text")
End Sub
注解
每个Windows窗体至少有一个BindingContext对象,用于管理窗体的 BindingManagerBase 对象。 由于类 BindingManagerBase 是抽象的,因此属性的 Item[] 返回类型为一个或一个 CurrencyManagerPropertyManager。 如果数据源是只能返回单个属性(而不是对象列表)的对象,则 Type 为一个 PropertyManager。 例如,如果指定一个 TextBox 作为数据源,则返回一个 PropertyManager 。 另一方面,如果数据源是实现 IList 的对象,或者 IBindingList返回一个 CurrencyManager 对象。
对于Windows窗体上的每个数据源,有单个 CurrencyManager 或 PropertyManager。 由于可能存在多个与Windows窗体关联的数据源,因此使用 BindingContext可以检索与数据源关联的任何特定CurrencyManager。
注释
使用该 Item[] 属性时,如果尚不存在,则 BindingContext 创建一个新 BindingManagerBase 属性。 这可能会导致一些混淆,因为返回的对象可能不管理列表(或任何列表)。 若要防止返回无效 BindingManagerBase,请使用 Contains 该方法确定预期 BindingManagerBase 是否已存在。
如果使用容器控件(例如GroupBox,PanelTabControl或)来包含数据绑定控件,则可以为该容器控件及其控件创建一个BindingContext。 然后,表单的每个部分都可以由其自身 BindingManagerBase管理。 BindingContext有关为同一数据源创建多个BindingManagerBase对象的详细信息,请参阅构造函数。
如果将控件添加到 TextBox 窗体并将其绑定到数据集中表的列,该控件将与 BindingContext 该窗体进行通信。 反过来,该 BindingContext数据关联会与具体 CurrencyManager 数据关联进行交谈。 如果查询了 Position 该 CurrencyManager控件的属性,它将报告该 TextBox 控件绑定的当前记录。 在下面的代码示例中,TextBox控件通过BindingContext数据集上的dataSet1表的列Customers绑定到FirstName它所打开的窗体上的表列。
TextBox1.DataBindings.Add("Text", dataSet1, "Customers.FirstName")
textBox1.DataBindings.Add("Text", dataSet1, "Customers.FirstName");
可以将第二 TextBox 个控件 (TextBox2) 添加到窗体,并将其 LastName 绑定到同一数据集中表的 Customers 列。 请注意 BindingContext 第一个绑定(TextBox1 到 Customers.FirstName),因此它将使用相同的 CurrencyManager绑定,因为两个文本框都绑定到同一数据集(DataSet1)。
TextBox2.DataBindings.Add("Text", dataSet1, "Customers.LastName")
textBox2.DataBindings.Add("Text", dataSet1, "Customers.LastName");
如果绑定到 TextBox2 其他数据集,则 BindingContext 创建和管理另一个 CurrencyManager数据集。
设置属性DisplayMember的方式DataSource必须保持一致;否则,BindingContext为同一数据集创建多个货币经理,这会导致错误。 下面的代码示例演示了设置属性及其关联 BindingContext 对象的几种方法。 只要在整个代码中保持一致,就可以使用以下任一方法设置属性。
ComboBox1.DataSource = DataSet1
ComboBox1.DisplayMember = "Customers.FirstName"
Me.BindingContext(dataSet1, "Customers").Position = 1
comboBox1.DataSource = DataSet1;
comboBox1.DisplayMember = "Customers.FirstName";
this.BindingContext[dataSet1, "Customers"].Position = 1;
ComboBox1.DataSource = DataSet1.Customers
ComboBox1.DisplayMember = "FirstName"
Me.BindingContext(dataSet1.Customers).Position = 1
comboBox1.DataSource = DataSet1.Customers;
comboBox1.DisplayMember = "FirstName";
this.BindingContext[dataSet1.Customers].Position = 1;
注释
大多数Windows 窗体应用程序通过 BindingSource 绑定。 该 BindingSource 组件封装 CurrencyManager 并公开 CurrencyManager 编程接口。 使用 BindingSource 绑定时,应使用公开 BindingSource 的成员来操作“货币”(即 Position),而不是通过 BindingContext。
构造函数
| 名称 | 说明 |
|---|---|
| BindingContext() |
初始化 BindingContext 类的新实例。 |
属性
| 名称 | 说明 |
|---|---|
| IsReadOnly |
获取一个值,该值指示集合是否为只读。 |
| Item[Object, String] |
获取与指定数据源和数据成员关联的项 BindingManagerBase 。 |
| Item[Object] |
BindingManagerBase获取与指定数据源关联的项。 |
方法
| 名称 | 说明 |
|---|---|
| Add(Object, BindingManagerBase) |
将 BindingManagerBase 与特定数据源关联的数据添加到集合中。 |
| AddCore(Object, BindingManagerBase) |
将 BindingManagerBase 与特定数据源关联的数据添加到集合中。 |
| Clear() |
清除任何 BindingManagerBase 对象的集合。 |
| ClearCore() |
清除集合。 |
| Contains(Object, String) |
获取一个值,该值 BindingContext 指示是否包含 BindingManagerBase 与指定数据源和数据成员关联的值。 |
| Contains(Object) |
获取一个值,该值 BindingContext 指示是否包含 BindingManagerBase 与指定数据源关联的值。 |
| Equals(Object) |
确定指定的对象是否等于当前对象。 (继承自 Object) |
| GetHashCode() |
用作默认哈希函数。 (继承自 Object) |
| GetType() |
获取当前实例的 Type。 (继承自 Object) |
| MemberwiseClone() |
创建当前 Object的浅表副本。 (继承自 Object) |
| OnCollectionChanged(CollectionChangeEventArgs) |
引发 CollectionChanged 事件。 |
| Remove(Object) |
BindingManagerBase删除与指定数据源关联的数据。 |
| RemoveCore(Object) |
删除 BindingManagerBase 与指定数据源关联的数据。 |
| ToString() |
返回一个表示当前对象的字符串。 (继承自 Object) |
| UpdateBinding(BindingContext, Binding) |
将 a Binding 与新 BindingContext关联。 |
活动
| 名称 | 说明 |
|---|---|
| CollectionChanged |
始终在处理时引发 NotImplementedException 。 |
显式接口实现
| 名称 | 说明 |
|---|---|
| ICollection.CopyTo(Array, Int32) |
从集合索引开始,将集合的元素复制到指定的数组中。 |
| ICollection.Count |
获取由 .BindingContext. 管理的对象总数CurrencyManager。 |
| ICollection.IsSynchronized |
获取一个值,该值指示集合是否同步。 |
| ICollection.SyncRoot |
获取用于同步的对象(线程安全)。 |
| IEnumerable.GetEnumerator() |
获取集合的枚举器。 |
扩展方法
| 名称 | 说明 |
|---|---|
| AsParallel(IEnumerable) |
启用查询的并行化。 |
| AsQueryable(IEnumerable) |
将 IEnumerable 转换为 IQueryable。 |
| Cast<TResult>(IEnumerable) |
将 IEnumerable 的元素强制转换为指定类型。 |
| OfType<TResult>(IEnumerable) |
根据指定类型筛选 IEnumerable 的元素。 |