BindingContext 类

定义

管理任何继承自 Control 类的对象的 BindingManagerBase 对象集合。

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个对象,用于将五个控件(a 和 4 个DateTimePickerTextBox控件)绑定到多个数据源。 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。 如果数据源是一个对象,该对象只能返回单个属性 (而不是) 的对象列表,则 TypePropertyManager。 例如,如果指定 TextBox 作为数据源, PropertyManager 则返回 。 另一方面,如果数据源是实现 IListIBindingList的对象, CurrencyManager 则返回 。

对于 Windows 窗体上的每个数据源,有一 CurrencyManager 个 或 PropertyManager。 由于可能有多个与 Windows 窗体关联的数据源, BindingContext 因此 你可以检索与数据源关联的任何特定 CurrencyManager 项。

备注

使用 Item[] 属性时, BindingContext 会创建一个新的 BindingManagerBase (如果尚不存在)。 这可能会导致一些混淆,因为返回的对象可能无法管理列表 (或你希望的任何列表) 。 若要防止返回无效 BindingManagerBase的 ,请使用 Contains 方法确定预期的 BindingManagerBase 是否已存在。

如果使用容器控件(如 GroupBoxPanelTabControl)来包含数据绑定控件,则可以仅为该容器控件及其控件创建 BindingContext 。 然后,表单的每个部分都可以由其自己的 BindingManagerBase管理。 有关为同一数据源创建多个BindingManagerBase对象的详细信息,BindingContext请参阅 构造函数。

如果将控件 TextBox 添加到窗体并将其绑定到数据集中表的列,则控件将与 BindingContext 该窗体的 通信。 反过来,会 BindingContext与该数据关联的特定 CurrencyManager 对话。 如果查询 了 PositionCurrencyManager属性,它将报告该 TextBox 控件绑定的当前记录。 在以下代码示例中TextBox,控件FirstName通过 BindingContext 数据集上的 dataSet1 绑定到表的Customers列,以表示它所采用的形式。

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

可以将第二 TextBox 个控件 (TextBox2) 添加到窗体中,并将其绑定到 LastName 同一数据集中表的 Customers 列。 知道 BindingContext 要) 的第一个绑定 (TextBox1Customers.FirstName ,因此它将使用相同的 CurrencyManager,因为两个文本框都绑定到同一数据集 (DataSet1) 。

TextBox2.DataBindings.Add("Text", dataSet1, "Customers.LastName")  
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;  
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绑定。 组件 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)

获取一个值,该值指示 BindingContext 是否包含与指定数据源关联的 BindingManagerBase

Contains(Object, String)

获取一个值,该值指示 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)

Binding 与一个新的 BindingContext 关联起来。

事件

CollectionChanged

被处理时总会引发 NotImplementedException

显式接口实现

ICollection.CopyTo(Array, Int32)

将集合中的元素复制到指定数组,从集合索引处开始。

ICollection.Count

获取 CurrencyManager 所管理的 BindingContext 对象的总数。

ICollection.IsSynchronized

获取一个值,通过该值指示该集合是否已同步。

ICollection.SyncRoot

获取用于同步(线程安全)的对象。

IEnumerable.GetEnumerator()

获取集合的枚举数。

扩展方法

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。

AsParallel(IEnumerable)

启用查询的并行化。

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryable

适用于

另请参阅