BindingContext 類別

定義

為繼承 BindingManagerBase 類別的任何物件管理 Control 物件集合。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 a 和四個控制項)系結至數個數據源。The following code example creates four Binding objects to bind five controls - a DateTimePicker and four TextBox controls - to several data sources. 接著會使用來取得每個資料來源的BindingManagerBaseBindingContextThe 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 form 至少有一個BindingContext物件可BindingManagerBase管理表單的物件。Each Windows Form has at least one BindingContext object that manages the BindingManagerBase objects for the form. 因為類別BindingManagerBase是抽象的,所以Item[Object]屬性的傳回型別PropertyManager會是CurrencyManager或。Because the BindingManagerBase class is abstract, the return type of the Item[Object] property is either a CurrencyManager or a PropertyManager. 如果資料來源是只能傳回單一屬性(而不是物件清單)的物件, TypePropertyManager是。If 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為數據源PropertyManager ,則會傳回。For example, if you specify a TextBox as the data source, a PropertyManager is returned. 另一方面,如果資料來源是執行IListIBindingList的物件, CurrencyManager則會傳回。On the other hand, if the data source is an object that implements IList or IBindingList, a CurrencyManager is returned.

對於 Windows Form 上的每個資料來源,只有CurrencyManager一個或。 PropertyManagerFor each data source on a Windows Form, there is a single CurrencyManager or PropertyManager. 因為可能有多個與 Windows Form 相關聯的資料來源, BindingContext所以可讓您取得與CurrencyManager資料來源相關聯的任何特定。Because 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]屬性時BindingContext ,會建立新BindingManagerBase的(如果尚未存在的話)。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.

如果您使用容器控制項( GroupBox例如、 PanelTabControl)來包含資料繫結控制項,您可以只BindingContext針對該容器控制項和其控制項建立。If 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. 如需BindingContext針對相同資料來源建立多個BindingManagerBase物件的詳細資訊,請參閱此函式。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. 接著,會與該資料關聯的特定CurrencyManager交談。 BindingContextThe BindingContext, in turn, talks to the specific CurrencyManager for that data association. 如果您查詢Position的屬性CurrencyManager,它會報告該TextBox控制項之系結的目前記錄。If you queried the Position property of the CurrencyManager, it would report the current record for the binding of that TextBox control. 在下列程式碼範例中, TextBox控制項是透過其所在FirstName表單的Customers BindingContext ,系結dataSet1至資料集上資料表的資料行。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()新增至表單LastName ,並將它系結Customers至相同資料集中資料表的資料行。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. Customers.FirstName TextBox1 DataSet1 CurrencyManager會知道第一個系結(至),因此它會使用相同的,因為這兩個文字方塊都會系結至相同的資料集()。 BindingContextThe 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 ,會建立並管理CurrencyManager第二個。If 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 Forms 應用程式都會BindingSource透過進行系結。Most Windows Forms applications bind through a BindingSource. 元件會封裝並公開CurrencyManager程式設計介面。 CurrencyManager BindingSourceThe BindingSource component encapsulates a CurrencyManager and exposes the CurrencyManager programming interface. 使用BindingSource進行系結時,您應該使用BindingSource由公開的成員來操作 "currency Position" (也就是),而不是BindingContext經過。When 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)

加入與特定資料來源相關的 BindingManagerBase 至集合中。Adds the BindingManagerBase associated with a specific data source to the collection.

AddCore(Object, BindingManagerBase)

加入與特定資料來源相關的 BindingManagerBase 至集合中。Adds 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()

傳回代表目前物件的字串。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.

適用於

另請參閱