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 個 物件,將五個 DateTimePicker 控制項和四 TextBox 個控制項系結至數個數據源。 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 Form 至少有一個 BindingContext 物件可管理 BindingManagerBase 表單的物件。 因為類別 BindingManagerBase 是抽象的,所以 屬性的 Item[] 傳回型別為 CurrencyManagerPropertyManager 。 如果資料來源是只能傳回單一屬性的物件 (,而不是) 的物件清單,則 TypePropertyManager 。 例如,如果您將 指定 TextBox 為數據源, PropertyManager 則會傳回 。 另一方面,如果資料來源是實作 IListIBindingList 的物件, CurrencyManager 則會傳回 。

針對 Windows Form 上的每個資料來源,會有單 CurrencyManager 一 或 PropertyManager 。 因為可能會有多個與 Windows Form 相關聯的資料來源,所以 BindingContext 可讓您擷取與資料來源相關聯的任何特定 CurrencyManager

注意

使用 Item[] 屬性時,如果尚未存在,就會 BindingContext 建立新的 BindingManagerBase 。 這可能會導致一些混淆,因為傳回的物件可能不會管理清單 (或任何您想要的清單) 。 若要防止傳回無效 BindingManagerBase 的 ,請使用 Contains 方法來判斷預定 BindingManagerBase 是否已經存在。

如果您使用容器控制項,例如 GroupBoxPanelTabControl 來包含資料繫結控制項,您可以只針對該容器控制項及其控制項建立 BindingContext 。 然後,表單的每個部分都可以由自己的 BindingManagerBase 管理。 BindingContext如需為相同資料來源建立多個 BindingManagerBase 物件的詳細資訊,請參閱 建構函式。

如果您將控制項新增 TextBox 至表單,並將它系結至資料集中資料表的資料行,控制項會與該表單的 通訊 BindingContext 。 接著,會 BindingContext 與該資料關聯的特定 CurrencyManager 交談。 如果您查詢 PositionCurrencyManager 屬性,它會報告該 TextBox 控制項系結的目前記錄。 在下列程式碼範例中, TextBox 控制項會透過它所在的表單,系 BindingContext 結至 FirstName 資料集上 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請注意第一個系結 () Customers.FirstNameTextBox1 ,因此會使用相同的 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 個 。

請務必一致地瞭解如何設定 DataSourceDisplayMember 屬性;否則, 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 Forms應用程式都會透過 系 BindingSource 結。 元件 BindingSourceCurrencyManager 封裝 並公開 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

適用於

另請參閱