BindingContext BindingContext BindingContext BindingContext Class

定義

為繼承 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
繼承
BindingContextBindingContextBindingContextBindingContext
實作

範例

下列程式碼範例會建立四個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再用來取得BindingManagerBase每個資料來源。The 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]屬性是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做為資料來源,PropertyManager會傳回。For example, if you specify a TextBox as the data source, a PropertyManager is returned. 另一方面,如果資料來源物件,實作IList或是IBindingListCurrencyManager會傳回。On the other hand, if the data source is an object that implements IList or IBindingList, a CurrencyManager is returned.

在 Windows Form 上每個資料來源,沒有單一CurrencyManagerPropertyManagerFor each data source on a Windows Form, there is a single CurrencyManager or PropertyManager. 因為可能有多個 Windows 表單時,相關聯的資料來源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.

如果您使用容器控制項,例如GroupBoxPanel,或TabControl,以包含資料繫結控制項,您可以建立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. 然後,您的表單的每個部分可將自己BindingManagerBaseThen, 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. BindingContext,,反過來特定CurrencyManager針對該資料關聯。The 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資料表上dataSet1資料集通過BindingContext其位於表單。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. BindingContext感知的第一個繫結 (TextBox1Customers.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 Forms 應用程式透過繫結BindingSourceMost Windows Forms applications bind through a BindingSource. BindingSource元件會封裝CurrencyManager,並公開CurrencyManager程式設計介面。The BindingSource component encapsulates a CurrencyManager and exposes the CurrencyManager programming interface. 使用時BindingSource繫結,您應該使用所公開的成員BindingSource操作 貨幣 而非經過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() BindingContext() BindingContext()

初始化 BindingContext 類別的新執行個體。Initializes a new instance of the BindingContext class.

屬性

IsReadOnly IsReadOnly IsReadOnly IsReadOnly

取得值,表示集合是否為唯讀。Gets a value indicating whether the collection is read-only.

Item[Object, String] Item[Object, String] Item[Object, String] Item[Object, String]

取得與指定之資料來源和資料成員相關的 BindingManagerBaseGets a BindingManagerBase that is associated with the specified data source and data member.

Item[Object] Item[Object] Item[Object] Item[Object]

取得與指定之資料來源相關的 BindingManagerBaseGets the BindingManagerBase that is associated with the specified data source.

方法

Add(Object, BindingManagerBase) Add(Object, BindingManagerBase) Add(Object, BindingManagerBase) Add(Object, BindingManagerBase)

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

AddCore(Object, BindingManagerBase) AddCore(Object, BindingManagerBase) AddCore(Object, BindingManagerBase) AddCore(Object, BindingManagerBase)

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

Clear() Clear() Clear() Clear()

清除任何 BindingManagerBase 物件的集合。Clears the collection of any BindingManagerBase objects.

ClearCore() ClearCore() ClearCore() ClearCore()

清除集合。Clears the collection.

Contains(Object) Contains(Object) Contains(Object) Contains(Object)

取得值,指出 BindingContext 是否包含與指定資料來源相關的 BindingManagerBaseGets a value indicating whether the BindingContext contains the BindingManagerBase associated with the specified data source.

Contains(Object, String) Contains(Object, String) Contains(Object, String) 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) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
OnCollectionChanged(CollectionChangeEventArgs) OnCollectionChanged(CollectionChangeEventArgs) OnCollectionChanged(CollectionChangeEventArgs) OnCollectionChanged(CollectionChangeEventArgs)

引發 CollectionChanged 事件。Raises the CollectionChanged event.

Remove(Object) Remove(Object) Remove(Object) Remove(Object)

刪除與指定資料來源相關的 BindingManagerBaseDeletes the BindingManagerBase associated with the specified data source.

RemoveCore(Object) RemoveCore(Object) RemoveCore(Object) RemoveCore(Object)

移除與指定資料來源相關聯的 BindingManagerBaseRemoves the BindingManagerBase associated with the specified data source.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)
UpdateBinding(BindingContext, Binding) UpdateBinding(BindingContext, Binding) UpdateBinding(BindingContext, Binding) UpdateBinding(BindingContext, Binding)

Binding 與新的 BindingContext 產生關聯。Associates a Binding with a new BindingContext.

事件

CollectionChanged CollectionChanged CollectionChanged CollectionChanged

處理時一定會引發 NotImplementedExceptionAlways raises a NotImplementedException when handled.

明確介面實作

ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32)

將集合的項目複製到指定的陣列中 (從集合索引開始)。Copies the elements of the collection into a specified array, starting at the collection index.

ICollection.Count ICollection.Count ICollection.Count ICollection.Count

取得由 CurrencyManager 管理的 BindingContext 物件總數。Gets the total number of CurrencyManager objects managed by the BindingContext.

ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized

取得值,指出是否同步處理集合。Gets a value indicating whether the collection is synchronized.

ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot

取得用來進行同步處理的物件 (安全執行緒)。Gets an object to use for synchronization (thread safety).

IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator()

取得此集合的列舉程式。Gets an enumerator for the collection.

擴充方法

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

啟用查詢的平行化作業。Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryableConverts an IEnumerable to an IQueryable.

適用於

另請參閱