BindingContext 类

定义

管理任何继承自 Control 类的对象的 BindingManagerBase 对象集合。Manages the collection of BindingManagerBase objects for any object that inherits from the Control class.

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 对象用于将五个控件(DateTimePicker 和四个 TextBox 控件)绑定到多个数据源。The following code example creates four Binding objects to bind five controls - a DateTimePicker and four TextBox controls - to several data sources. 然后,使用 BindingContext 获取每个数据源的 BindingManagerBaseThe BindingContext is then used to get the BindingManagerBase for each data source.

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 窗体至少具有一个管理窗体的 BindingManagerBase 对象的 BindingContext 对象。Each Windows Form has at least one BindingContext object that manages the BindingManagerBase objects for the form. 由于 BindingManagerBase 类是抽象的,因此 Item[Object] 属性的返回类型为 CurrencyManagerPropertyManagerBecause the BindingManagerBase class is abstract, the return type of the Item[Object] property is either a CurrencyManager or a PropertyManager. 如果数据源是只能返回单个属性的对象(而不是对象的列表),则 TypePropertyManagerIf the data source is an object that can return only a single property (instead of a list of objects), the Type is a PropertyManager. 例如,如果指定 TextBox 作为数据源,则返回 PropertyManagerFor example, if you specify a TextBox as the data source, a PropertyManager is returned. 另一方面,如果数据源是实现 IListIBindingList的对象,则返回 CurrencyManagerOn the other hand, if the data source is an object that implements IList or IBindingList, a CurrencyManager is returned.

对于 Windows 窗体上的每个数据源,都有单个 CurrencyManagerPropertyManagerFor each data source on a Windows Form, there is a single CurrencyManager or PropertyManager. 由于可能存在多个与 Windows 窗体关联的数据源,因此 BindingContext 允许您检索与数据源关联的任何特定 CurrencyManagerBecause there may be multiple data sources associated with a Windows Form, the BindingContext enables you to retrieve any particular CurrencyManager associated with a data source.

备注

使用 Item[Object] 属性时,如果不存在新 BindingManagerBase,则 BindingContext 会创建一个新的。When using the Item[Object] property, the BindingContext creates a new BindingManagerBase if one does not already exist. 这可能会导致一些混乱,因为返回的对象可能不会管理你打算的列表(或任何列表)。This can lead to some confusion, as the returned object may not manage the list (or any list) that you intend. 若要防止返回无效的 BindingManagerBase,请使用 Contains 方法来确定预期 BindingManagerBase 是否已存在。To prevent returning an invalid BindingManagerBase, use the Contains method to determine if the intended BindingManagerBase already exists.

如果使用容器控件(例如 GroupBoxPanelTabControl)来包含数据绑定控件,则可以只为该容器控件及其控件创建 BindingContextIf you use a container control, such as a GroupBox, Panel, or TabControl, to contain data-bound controls, you can create a BindingContext for just that container control and its controls. 然后,窗体的每个部分都可以由其自己的 BindingManagerBase管理。Then, each part of your form can be managed by its own BindingManagerBase. 有关为同一数据源创建多个 BindingManagerBase 对象的详细信息,请参阅 BindingContext 构造函数。See the BindingContext constructor for more information about creating multiple BindingManagerBase objects for the same data source.

如果将 TextBox 控件添加到窗体,并将其绑定到数据集中的表的列,则该控件将与该窗体的 BindingContext 通信。If you add a TextBox control to a form and bind it to a column of a table in a dataset, the control communicates with the BindingContext of that form. BindingContext将与该数据关联的特定 CurrencyManager 进行通信。The BindingContext, in turn, talks to the specific CurrencyManager for that data association. 如果您查询了 CurrencyManagerPosition 属性,它将报告该 TextBox 控件的绑定的当前记录。If you queried the Position property of the CurrencyManager, it would report the current record for the binding of that TextBox control. 在下面的代码示例中,TextBox 控件通过其所基于的窗体的 BindingContext 绑定到 dataSet1 数据集中的 Customers 表的 FirstName 列。In the following code example, a TextBox control is bound to the FirstName column of a Customers table on the dataSet1 dataset through the BindingContext for the form it is on.

TextBox1.DataBindings.Add("Text", dataSet1, "Customers.FirstName")  
  
textBox1.DataBindings.Add("Text", dataSet1, "Customers.FirstName");  
  
textBox1->DataBindings->Add("Text", dataSet1, "Customers.FirstName");  

您可以向窗体中添加第二个 TextBox 控件(TextBox2),并将其绑定到同一个数据集中 Customers 表的 LastName 列。You can add a second TextBox control (TextBox2) to the form and bind it to the LastName column of the Customers table in the same dataset. BindingContext 知道第一个绑定(TextBox1 Customers.FirstName),因此它将使用相同的 CurrencyManager,因为这两个文本框都绑定到同一个数据集(DataSet1)。The BindingContext is aware of the first binding (TextBox1 to Customers.FirstName), so it would use the same CurrencyManager, as both text boxes are bound to the same dataset (DataSet1).

TextBox2.DataBindings.Add("Text", dataSet1, "Customers.LastName")  
  
textBox2.DataBindings.Add("Text", dataSet1, "Customers.LastName");  
  
textBox2->DataBindings->Add("Text", dataSet1, "Customers.LastName");  

如果将 TextBox2 绑定到不同的数据集,则 BindingContext 创建并管理第二个 CurrencyManagerIf you bind TextBox2 to a different dataset, the BindingContext creates and manages a second CurrencyManager.

务必要一致地设置 DataSourceDisplayMember 属性;否则,BindingContext 将为同一个数据集创建多个货币管理器,这将导致错误。It is important to be consistent about how you set the DataSource and DisplayMember properties; otherwise, the BindingContext creates multiple currency managers for the same dataset, which results in errors. 下面的代码示例演示了几种设置属性及其关联 BindingContext 对象的方法。The following code example shows a few ways to set the properties and their associated BindingContext objects. 您可以使用以下任一方法来设置属性,前提是您在代码中保持一致。You can set the properties using either of the following methods, as long as you are consistent throughout your code.

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;  
comboBox1->DisplayMember = "Customers.FirstName";  
this->BindingContext->get_Item(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;  
  
comboBox1->DataSource = dataSet1->Customers;  
comboBox1->DisplayMember = "FirstName";  
this->BindingContext->get_Item(dataSet1->Customers)->Position = 1;  
  

备注

大多数 Windows 窗体的应用程序通过 BindingSource进行绑定。Most Windows Forms applications bind through a BindingSource. BindingSource 组件封装了 CurrencyManager 并公开了 CurrencyManager 编程接口。The BindingSource component encapsulates a CurrencyManager and exposes the CurrencyManager programming interface. 使用 BindingSource 进行绑定时,应使用 BindingSource 公开的成员操作 "currency" (即 Position),而不是通过 BindingContextWhen using a BindingSource for binding, you should use the members exposed by the BindingSource to manipulate "currency" (that is, Position) rather than go through the BindingContext.

构造函数

BindingContext()

初始化 BindingContext 类的新实例。Initializes a new instance of the BindingContext class.

属性

IsReadOnly

获取一个值,该值指示集合是否为只读。Gets a value indicating whether the collection is read-only.

Item[Object, String]

获取与指定数据源和数据成员相关联的一个 BindingManagerBaseGets a BindingManagerBase that is associated with the specified data source and data member.

Item[Object]

获取与指定数据源关联的 BindingManagerBaseGets the BindingManagerBase that is associated with the specified data source.

方法

Add(Object, BindingManagerBase)

向集合添加与特定数据源关联的 BindingManagerBaseAdds the BindingManagerBase associated with a specific data source to the collection.

AddCore(Object, BindingManagerBase)

向集合添加与特定数据源关联的 BindingManagerBaseAdds the BindingManagerBase associated with a specific data source to the collection.

Clear()

清除所有 BindingManagerBase 对象的集合。Clears the collection of any BindingManagerBase objects.

ClearCore()

清除集合。Clears the collection.

Contains(Object)

获取一个值,该值指示 BindingContext 是否包含与指定数据源关联的 BindingManagerBaseGets a value indicating whether the BindingContext contains the BindingManagerBase associated with the specified data source.

Contains(Object, String)

获取一个值,该值指示 BindingContext 是否包含与指定数据源和数据成员关联的 BindingManagerBaseGets a value indicating whether the BindingContext contains the BindingManagerBase associated with the specified data source and data member.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
OnCollectionChanged(CollectionChangeEventArgs)

引发 CollectionChanged 事件。Raises the CollectionChanged event.

Remove(Object)

删除与指定数据源关联的 BindingManagerBaseDeletes the BindingManagerBase associated with the specified data source.

RemoveCore(Object)

移除与指定数据源关联的 BindingManagerBaseRemoves the BindingManagerBase associated with the specified data source.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)
UpdateBinding(BindingContext, Binding)

Binding 与一个新的 BindingContext 关联起来。Associates a Binding with a new BindingContext.

事件

CollectionChanged

被处理时总会引发 NotImplementedExceptionAlways raises a NotImplementedException when handled.

显式接口实现

ICollection.CopyTo(Array, Int32)

将集合中的元素复制到指定数组,从集合索引处开始。Copies the elements of the collection into a specified array, starting at the collection index.

ICollection.Count

获取 CurrencyManager 所管理的 BindingContext 对象的总数。Gets the total number of CurrencyManager objects managed by the BindingContext.

ICollection.IsSynchronized

获取一个值,通过该值指示该集合是否已同步。Gets a value indicating whether the collection is synchronized.

ICollection.SyncRoot

获取用于同步(线程安全)的对象。Gets an object to use for synchronization (thread safety).

IEnumerable.GetEnumerator()

获取此集合的枚举器。Gets an enumerator for the collection.

扩展方法

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

适用于

另请参阅