BindingManagerBase 类

管理绑定到相同数据源和数据成员的所有 Binding 对象。该类为抽象类。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public MustInherit Class BindingManagerBase
用法
Dim instance As BindingManagerBase
public abstract class BindingManagerBase
public ref class BindingManagerBase abstract
public abstract class BindingManagerBase
public abstract class BindingManagerBase

备注

使用 BindingManagerBase,可以对 Windows 窗体上绑定到相同数据源的数据绑定控件进行同步。(有关将控件简单绑定到数据源的更多信息,请参见 Binding 类。)例如,假定窗体包含两个绑定到相同数据源的不同列的 TextBox 控件。数据源可能是一个包含客户姓名的 DataTable,其中的列可能包含名字和姓氏。这两个控件必须同步以便一起显示同一客户的正确姓名。从 BindingManagerBase 类继承的 CurrencyManager 通过维护指向数据源中当前项的指针来完成此同步。TextBox 控件被绑定到当前项,因此它们显示同一行的信息。在当前项更改时,CurrencyManager 通知所有绑定控件,以便它们能够刷新它们的数据。此外,可以设置 Position 属性来指定控件所指向的 DataTable 中的行。若要确定数据源中存在的行数,请使用 Count 属性。

CurrencyManager 很有必要,因为数据源未必维护当前项的指针。例如,数组和 ArrayList 对象可以是数据源,但它们没有返回当前项的属性。若要获取当前项,请使用 Current 属性。

PropertyManager 也从 BindingManagerBase 继承,它用于维护对象的当前属性,而不是数据源中当前对象的属性。因此,试图为 PropertyManager 设置 Position 属性或 Count 属性无效。

若要创建 BindingManagerBase,请使用 BindingContext 类,该类根据所管理的数据源返回 CurrencyManagerPropertyManager

解决方案程序员最好将控件直接绑定到 BindingSource 组件,这个组件同时作为数据源和实际目标数据源的数据连接器。BindingSource 极大地简化了简单数据绑定和复杂数据绑定,包括管理控件与其目标之间的流量。

给继承者的说明 当从 BindingManagerBase 继承时,必须重写下列抽象成员:AddNewCountCancelCurrentEditCurrentEndCurrentEditGetItemPropertiesOnCurrentChangedPositionRemoveAtResumeBindingSuspendBindingUpdateIsBinding

示例

下面的代码示例使用 BindingContext 返回特定数据源的 BindingManagerBase。(示例假定您已在模块的“声明”部分中声明了)myBindingManagerBase。然后示例向 CurrentChangedPositionChanged 事件添加事件委托。最后,示例包含四个方法(MoveNextMovePreviousMoveFirstMoveLast),它们增加或减小 Position 属性,并将 Position 设置为列表中的第一行或最后一行。列表中的最后一行通过 Count 属性来确定。

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
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;
}
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.get_BindingContext().get_Item(dataSet1, 
        "Customers.CustomersToOrders");

    // Adds delegates to the CurrentChanged and PositionChanged events.
    myBindingManagerBase.add_PositionChanged(new EventHandler
        (BindingManagerBase_PositionChanged));
    myBindingManagerBase.add_CurrentChanged(new EventHandler
        (BindingManagerBase_CurrentChanged));
} //GetBindingManagerBase

private void BindingManagerBase_PositionChanged(Object sender, EventArgs e)
{
    // Prints the new Position of the BindingManagerBase.
    Console.Write("Position Changed: ");
    Console.WriteLine(((BindingManagerBase)(sender)).get_Position());
} //BindingManagerBase_PositionChanged

private void BindingManagerBase_CurrentChanged(Object sender, EventArgs e)
{
    // Prints the new value of the current object.
    Console.Write("Current Changed: ");
    Console.WriteLine(((BindingManagerBase)(sender)).get_Current());
} //BindingManagerBase_CurrentChanged

private void MoveNext()
{
    // Increments the Position property value by one.
    myBindingManagerBase.set_Position(myBindingManagerBase.get_Position()
        + 1);
} //MoveNext

private void MovePrevious()
{
    // Decrements the Position property value by one.
    myBindingManagerBase.set_Position(myBindingManagerBase.get_Position()
        - 1);
} //MovePrevious

private void MoveFirst()
{
    // Goes to the first row in the list.
    myBindingManagerBase.set_Position(0);
} //MoveFirst

private void MoveLast()
{
    // Goes to the last row in the list.
    myBindingManagerBase.set_Position(myBindingManagerBase.get_Count() - 1);
} //MoveLast

继承层次结构

System.Object
  System.Windows.Forms.BindingManagerBase
     System.Windows.Forms.CurrencyManager
     System.Windows.Forms.PropertyManager

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

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

请参见

参考

BindingManagerBase 成员
System.Windows.Forms 命名空间
BindingSource
BindingContext 类
CurrencyManager
PropertyManager