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 物件,將五個控制項(a 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[]CurrencyManagerPropertyManagerBecause the BindingManagerBase class is abstract, the return type of the Item[] 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 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[]BindingContext 會建立新的( BindingManagerBase 如果不存在的話)。When using the Item[] 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 )來包含資料繫結控制項,則可以 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 ,它會報告該控制項系結的目前記錄 TextBoxIf 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 資料行 BindingContextIn 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可感知) 的第一個系結 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. 下列程式碼範例會示範設定屬性及其相關聯物件的幾種方式 BindingContextThe 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 來操作「貨幣」 (也就是 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)

加入與特定資料來源相關的 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.

適用於

另請參閱