BindingManagerBase 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
管理绑定到相同数据源和数据成员的所有 Binding 对象。 此类为抽象类。
public ref class BindingManagerBase abstract
public abstract class BindingManagerBase
type BindingManagerBase = class
Public MustInherit Class BindingManagerBase
- 继承
-
BindingManagerBase
- 派生
示例
下面的代码示例使用 BindingContext 返回 BindingManagerBase 特定数据源的 。 (该示例假定已在 module 的声明部分声明 myBindingManagerBase
。) 然后,该示例将事件委托添加到 CurrentChanged 和 PositionChanged 事件。 最后,该示例包含四个方法,MoveNext
(、 MoveFirst
MovePrevious
、 和 MoveLast
) ,用于递增或递Position减属性,并将 设置为Position列表中的第一行或最后一行。 列表中的最后一行通过使用 Count 属性确定。
void GetBindingManagerBase()
{
/* CustomersToOrders is the RelationName of a DataRelation.
Therefore, the list maintained by the BindingManagerBase is the
list of orders that belong to a specific customer in the
DataTable named Customers, found in DataSet1. */
myBindingManagerBase = this->BindingContext[DataSet1, "Customers.CustomersToOrders"];
// Adds delegates to the CurrentChanged and PositionChanged events.
myBindingManagerBase->PositionChanged += gcnew EventHandler( this, &Form1::BindingManagerBase_PositionChanged );
myBindingManagerBase->CurrentChanged += gcnew EventHandler( this, &Form1::BindingManagerBase_CurrentChanged );
}
void BindingManagerBase_PositionChanged( Object^ sender, EventArgs^ /*e*/ )
{
// Prints the new Position of the BindingManagerBase.
Console::Write( "Position Changed: " );
Console::WriteLine( (dynamic_cast<BindingManagerBase^>(sender))->Position );
}
void BindingManagerBase_CurrentChanged( Object^ sender, EventArgs^ /*e*/ )
{
// Prints the new value of the current object.
Console::Write( "Current Changed: " );
Console::WriteLine( (dynamic_cast<BindingManagerBase^>(sender))->Current );
}
void MoveNext()
{
// Increments the Position property value by one.
myBindingManagerBase->Position = myBindingManagerBase->Position + 1;
}
void MovePrevious()
{
// Decrements the Position property value by one.
myBindingManagerBase->Position = myBindingManagerBase->Position - 1;
}
void MoveFirst()
{
// Goes to the first row in the list.
myBindingManagerBase->Position = 0;
}
void MoveLast()
{
// Goes to the last row in the list.
myBindingManagerBase->Position = myBindingManagerBase->Count - 1;
}
private void GetBindingManagerBase()
{
/* CustomersToOrders is the RelationName of a DataRelation.
Therefore, the list maintained by the BindingManagerBase is the
list of orders that belong to a specific customer in the
DataTable named Customers, found in DataSet1. */
myBindingManagerBase =
this.BindingContext[DataSet1, "Customers.CustomersToOrders"];
// Adds delegates to the CurrentChanged and PositionChanged events.
myBindingManagerBase.PositionChanged +=
new EventHandler(BindingManagerBase_PositionChanged);
myBindingManagerBase.CurrentChanged +=
new EventHandler(BindingManagerBase_CurrentChanged);
}
private void BindingManagerBase_PositionChanged
(object sender, EventArgs e)
{
// Prints the new Position of the BindingManagerBase.
Console.Write("Position Changed: ");
Console.WriteLine(((BindingManagerBase)sender).Position);
}
private void BindingManagerBase_CurrentChanged
(object sender, EventArgs e)
{
// Prints the new value of the current object.
Console.Write("Current Changed: ");
Console.WriteLine(((BindingManagerBase)sender).Current);
}
private void MoveNext()
{
// Increments the Position property value by one.
myBindingManagerBase.Position += 1;
}
private void MovePrevious()
{
// Decrements the Position property value by one.
myBindingManagerBase.Position -= 1;
}
private void MoveFirst()
{
// Goes to the first row in the list.
myBindingManagerBase.Position = 0;
}
private void MoveLast()
{
// Goes to the last row in the list.
myBindingManagerBase.Position =
myBindingManagerBase.Count - 1;
}
Private Sub GetBindingManagerBase
' CustomersToOrders is the RelationName of a DataRelation.
' Therefore, the list maintained by the BindingManagerBase is the
' list of orders that belong to a specific customer in the
' DataTable named Customers, found in DataSet.
myBindingManagerBase = Me.BindingContext(DataSet1, _
"Customers.CustomersToOrders")
' Adds delegates to the CurrentChanged and PositionChanged events.
AddHandler myBindingManagerBase.PositionChanged, _
AddressOf BindingManagerBase_PositionChanged
AddHandler myBindingManagerBase.CurrentChanged, _
AddressOf BindingManagerBase_CurrentChanged
End Sub
Private Sub BindingManagerBase_PositionChanged _
(sender As Object, e As EventArgs)
' Prints the new Position of the BindingManagerBase.
Console.Write("Position Changed: ")
Console.WriteLine(CType(sender, BindingManagerBase).Position)
End Sub
Private Sub BindingManagerBase_CurrentChanged _
(sender As Object, e As EventArgs)
' Prints the new value of the current object.
Console.Write("Current Changed: ")
Console.WriteLine(CType(sender, BindingManagerBase).Current)
End Sub
Private Sub MoveNext
' Increments the Position property value by one.
myBindingManagerBase.Position += 1
End Sub
Private Sub MovePrevious
' Decrements the Position property value by one.
myBindingManagerBase.Position -= 1
End Sub
Private Sub MoveFirst
' Goes to the first row in the list.
myBindingManagerBase.Position = 0
End Sub
Private Sub MoveLast
' Goes to the last row in the list.
myBindingManagerBase.Position = _
myBindingManagerBase.Count - 1
End Sub
注解
启用 BindingManagerBase 绑定到同一数据源的 Windows 窗体上的数据绑定控件的同步。 (有关将控件简单绑定到数据源的详细信息,请参阅 Binding class.) 例如,假设窗体包含绑定到同一数据源但绑定到不同列的两 TextBox 个控件。 数据源可能是包含 DataTable 客户姓名的 ,而列可能包含名字和姓氏。 必须同步这两个控件,以便为同一客户一起显示正确的名字和姓氏。 CurrencyManager从 BindingManagerBase 类继承的 (通过维护指向数据源中当前项的指针)来实现此同步。 控件 TextBox 绑定到当前项,因此它们显示同一行的信息。 当当前项发生更改时, CurrencyManager 会通知所有绑定控件,以便它们可以刷新其数据。 此外,还可以设置 Position 属性以指定控件指向的 中的 DataTable 行。 若要确定数据源中存在的行数,请使用 Count 属性。
是必需的, CurrencyManager 因为数据源不一定维护当前项指针。 例如,数组和 ArrayList 对象可以是数据源,但它们没有返回当前项的属性。 若要获取当前项,请使用 Current 属性。
PropertyManager还继承自 BindingManagerBase,它用于维护 对象的当前属性,而不是数据源中当前对象的 属性。 因此,尝试为 PropertyManager 设置 Position 或 Count 属性不起作用。
若要创建 BindingManagerBase,请使用 BindingContext 类,该类返回 CurrencyManager 或 PropertyManager,具体取决于所管理的数据源。
建议解决方案程序员将控件直接 BindingSource 绑定到组件,组件既充当数据源,又充当实际目标数据源的数据连接器。 BindingSource 大大简化了简单和复杂的数据绑定,包括管理控件与其目标之间的货币。
实施者说明
从 BindingManagerBase继承时,必须重写以下抽象成员:AddNew()、、Count、CancelCurrentEdit()、EndCurrentEdit()Current、OnCurrentChanged(EventArgs)GetItemProperties()Position、RemoveAt(Int32)、、ResumeBinding()、、 SuspendBinding()和 。UpdateIsBinding()
构造函数
BindingManagerBase() |
初始化 BindingManagerBase 类的新实例。 |
字段
onCurrentChangedHandler |
为 CurrentChanged 事件指定事件处理程序。 |
onPositionChangedHandler |
为 PositionChanged 事件指定事件处理程序。 |
属性
Bindings |
获取所管理绑定的集合。 |
Count |
在派生类中重写时,获取 BindingManagerBase 托管的行数。 |
Current |
当在派生类中被重写时,获取当前对象。 |
IsBindingSuspended |
获取一个值,该值指示绑定是否已挂起。 |
Position |
当在派生类中被重写时,获取或设置绑定到该数据源的控件所指向的基础列表中的位置。 |
方法
事件
BindingComplete |
在数据绑定操作完成时发生。 |
CurrentChanged |
在当前绑定项更改时发生。 |
CurrentItemChanged |
在当前绑定项的状态更改时发生。 |
DataError |
当 Exception 被 BindingManagerBase 处理而未给出任何提示时发生。 |
PositionChanged |
在 Position 属性的值更改后发生。 |