Control.BindingContext 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定控制項的 BindingContext。
public:
virtual property System::Windows::Forms::BindingContext ^ BindingContext { System::Windows::Forms::BindingContext ^ get(); void set(System::Windows::Forms::BindingContext ^ value); };
[System.ComponentModel.Browsable(false)]
public virtual System.Windows.Forms.BindingContext BindingContext { get; set; }
[System.ComponentModel.Browsable(false)]
public virtual System.Windows.Forms.BindingContext? BindingContext { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.BindingContext : System.Windows.Forms.BindingContext with get, set
Public Overridable Property BindingContext As BindingContext
屬性值
控制項的 BindingContext。
實作
- 屬性
範例
下列程式碼範例會建立四 Binding 個 物件,將五個控制項和四 TextBox 個控制項系結至數個 DateTimePicker 資料來源。 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
備註
BindingContext的 Control 是用來傳回 由 所包含之所有資料繫結控制項的單 Control 一 BindingManagerBase 。 會 BindingManagerBase 讓系結至相同資料來源的所有控制項保持同步。 例如,設定 Position 的 BindingManagerBase 屬性會指定基礎清單中所有資料繫結控制項指向的專案。
如需建立新的 BindingContext 並將它指派給 BindingContext 屬性的詳細資訊,請參閱 BindingContext 。
給繼承者的注意事項
在衍生類別中覆 BindingContext 寫屬性時,請使用基類的 BindingContext 屬性來擴充基底實作。 否則,您必須提供所有實作。 您不需要同時覆寫 get
屬性的 BindingContext 和 set
存取子;您可以視需要只覆寫一個。