Control.BindingContext 属性
获取或设置控件的 BindingContext。
**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)
语法
声明
Public Overridable Property BindingContext As BindingContext
用法
Dim instance As Control
Dim value As BindingContext
value = instance.BindingContext
instance.BindingContext = value
public virtual BindingContext BindingContext { get; set; }
public:
virtual property BindingContext^ BindingContext {
BindingContext^ get ();
void set (BindingContext^ value);
}
/** @property */
public BindingContext get_BindingContext ()
/** @property */
public void set_BindingContext (BindingContext value)
public function get BindingContext () : BindingContext
public function set BindingContext (value : BindingContext)
属性值
控件的 BindingContext。
备注
Control 的 BindingContext 用于为 Control 包含的所有数据绑定控件返回单个 BindingManagerBase。BindingManagerBase 使绑定到同一数据源的所有控件保持同步。例如,设置 BindingManagerBase 的 Position 属性可指定基础列表中所有数据绑定控件指向的项。
有关创建新的 BindingContext 以及将它分配到 BindingContext 属性的更多信息,请参见 BindingContext。
给继承者的说明 在派生类中重写 BindingContext 属性时,请使用基类的 BindingContext 属性来扩展基实现。否则,您必须提供所有实现。不需要同时重写 BindingContext 属性的 get 和 set 访问器;如果需要,可以只重写其中一个访问器。
示例
下面的代码示例创建四个 Binding 对象,将五个控件(一个 DateTimePicker 和四个 TextBox 控件)绑定到多个数据源。然后使用 BindingContext 为每个数据源获取 BindingManagerBase。
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
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");
}
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.get_DataBindings().Add(new Binding("Text", ds,
"customers.custName"));
text2.get_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.get_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.add_Parse(new ConvertEventHandler(CurrencyStringToDecimal));
b.add_Format(new ConvertEventHandler(DecimalToCurrencyString));
text3.get_DataBindings().Add(b);
// Get the BindingManagerBase for the Customers table.
bmCustomers = this.get_BindingContext().get_Item(ds, "Customers");
/* Get the BindingManagerBase for the Orders table using the
RelationName.
*/
bmOrders = this.get_BindingContext().get_Item(ds,
"customers.CustToOrders");
/* Bind the fourth TextBox control's Text property to the
third control's Text property.
*/
text4.get_DataBindings().Add("Text", text3, "Text");
} //BindControls
protected function 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. */
var b : Binding = new Binding
("Text", ds, "customers.custToOrders.OrderAmount");
b.add_Parse(CurrencyStringToDecimal);
b.add_Format(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"];
}
private function DecimalToCurrencyString(sender, cevent : ConvertEventArgs)
{
/* This method is the Format event handler. Whenever the
control displays a new value, the value is converted from
its native Decimal type to a string. The ToString method
then formats the value as a Currency, by using the
formatting character "c". */
// We can only convert to string type.
if(cevent.DesiredType != String.GetType()) return;
cevent.Value = (Decimal(cevent.Value)).ToString("c");
}
private function CurrencyStringToDecimal(sender, cevent : ConvertEventArgs)
{
/* This method is the Parse event-handler. The Parse event
occurs whenever the displayed value changes. The static
ToDecimal method of the Convert class converts the
value back to its native Decimal type. */
// Can only convert to Decimal type.
if(cevent.DesiredType != Decimal.GetType()) return;
cevent.Value = Decimal.Parse(cevent.Value.ToString(),
NumberStyles.Currency, null);
/* To see that no precision is lost, print the unformatted
value. For example, changing a value to "10.0001"
causes the control to display "10.00", but the
unformatted value remains "10.0001". */
Console.WriteLine(cevent.Value);
}
protected function button1_Click(sender, e : System.EventArgs)
{
// Go to the previous item in the Customer list.
bmCustomers.Position -= 1;
}
protected function button2_Click(sender, e : System.EventArgs)
{
// Go to the next item in the Customer list.
bmCustomers.Position += 1;
}
protected function button3_Click(sender, e : System.EventArgs)
{
// Go to the previous item in the Orders list.
bmOrders.Position-=1;
}
protected function button4_Click(sender, e : System.EventArgs)
{
// Go to the next item in the Orders list.
bmOrders.Position+=1;
}
// Create a DataSet with two tables and populate it.
private function MakeDataSet()
{
// Create a DataSet.
ds = new DataSet("myDataSet");
// Create two DataTables.
var tCust : DataTable = new DataTable("Customers");
var tOrders : DataTable= new DataTable("Orders");
// Create two columns, and add them to the first table.
var cCustID : DataColumn = new DataColumn("CustID", Int32);
var cCustName : DataColumn = new DataColumn("CustName");
tCust.Columns.Add(cCustID);
tCust.Columns.Add(cCustName);
// Create three columns, and add them to the second table.
var cID : DataColumn =
new DataColumn("CustID", Int32);
var cOrderDate : DataColumn =
new DataColumn("orderDate", DateTime);
var cOrderAmount : DataColumn =
new DataColumn("OrderAmount", Decimal);
tOrders.Columns.Add(cOrderAmount);
tOrders.Columns.Add(cID);
tOrders.Columns.Add(cOrderDate);
// Add the tables to the DataSet.
ds.Tables.Add(tCust);
ds.Tables.Add(tOrders);
// Create a DataRelation, and add it to the DataSet.
var dr : DataRelation = new DataRelation
("custToOrders", cCustID , cID);
ds.Relations.Add(dr);
/* Populate the tables. For each customer and order,
create need two DataRow variables. */
var newRow1 : DataRow;
var newRow2 : DataRow;
// Create three customers in the Customers Table.
for(var i : int = 1; i < 4; i++)
{
newRow1 = tCust.NewRow();
newRow1["custID"] = i;
// Add the row to the Customers table.
tCust.Rows.Add(newRow1);
}
// Give each customer a distinct name.
tCust.Rows[0]["custName"] = "Alpha";
tCust.Rows[1]["custName"] = "Beta";
tCust.Rows[2]["custName"] = "Omega";
// For each customer, create five rows in the Orders table.
for(var j : int = 1; j < 4; j++)
{
for(var k : int = 1; k < 6; k++)
{
newRow2 = tOrders.NewRow();
newRow2["CustID"]= j;
newRow2["orderDate"]= new DateTime(2001, j, k * 2);
newRow2["OrderAmount"] = j * 10 + k * .1;
// Add the row to the Orders table.
tOrders.Rows.Add(newRow2);
}
}
}
}
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0
请参见
参考
Control 类
Control 成员
System.Windows.Forms 命名空间
BindingContextChanged
Binding 类
BindingManagerBase 类