BindingManagerBase Class

Definition

管理绑定到相同数据源和数据成员的所有 Binding 对象。Manages all Binding objects that are bound to the same data source and data member. 此类是抽象类。This class is abstract.

public ref class BindingManagerBase abstract
public abstract class BindingManagerBase
type BindingManagerBase = class
Public MustInherit Class BindingManagerBase
Inheritance
BindingManagerBase
Derived

Examples

下面的代码示例使用 BindingContext 返回特定数据源的 BindingManagerBaseThe following code example uses the BindingContext to return a BindingManagerBase for a specific data source. (该示例假设已在模块的 "声明" 部分中声明 myBindingManagerBase。)然后,该示例将事件委托添加到 CurrentChangedPositionChanged 事件。(The example assumes you have declared myBindingManagerBase in the Declarations section of the module.) The example then adds event delegates to the CurrentChanged and PositionChanged events. 最后,该示例包含四个方法(MoveNextMovePreviousMoveFirstMoveLast),该方法递增或递减 Position 属性,并将 Position 设置为列表中的第一行或最后一行。Lastly, the example contains four methods (MoveNext, MovePrevious, MoveFirst, and MoveLast) that increment or decrement the Position property, and set the Position to the first or last row in the list. 使用 Count 属性确定列表中的最后一行。The last row in the list is determined by using the Count property.

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

Remarks

BindingManagerBase 可以在绑定到同一个数据源的 Windows 窗体上同步数据绑定控件。The BindingManagerBase enables the synchronization of data-bound controls on a Windows Form that are bound to the same data source. (有关将控件简单绑定到数据源的详细信息,请参阅 Binding 类。)例如,假设某个窗体包含两个绑定到同一个数据源但不同列的 TextBox 控件。(For more information about simple binding a control to a data source, see the Binding class.) For example, suppose that a form contains two TextBox controls that are bound to the same data source but to different columns. 数据源可能是包含客户名称的 DataTable,而列可能包含名字和姓氏。The data source might be a DataTable that contains customer names, while the columns might contain the first and last names. 必须同步两个控件,才能为同一客户显示正确的名字和姓氏。The two controls must be synchronized in order to display the correct first and last names together for the same customer. BindingManagerBase 类继承的 CurrencyManager通过保持指向数据源中的当前项的指针来完成此同步。The CurrencyManager, which inherits from the BindingManagerBase class, accomplishes this synchronization by maintaining a pointer to the current item in the data source. TextBox 控件将绑定到当前项,因此它们显示同一行的信息。The TextBox controls are bound to the current item so they display the information for the same row. 当当前项发生更改时,CurrencyManager 通知所有绑定控件,以便这些控件可以刷新其数据。When the current item changes, the CurrencyManager notifies all the bound controls so that they can refresh their data. 此外,还可以设置 Position 属性以指定控件所指向的 DataTable 中的行。Furthermore, you can set the Position property to specify the row in the DataTable that the controls point to. 若要确定数据源中存在的行数,请使用 Count 属性。To determine how many rows exist in the data source, use the Count property.

CurrencyManager 是必需的,因为数据源不必维护当前项指针。The CurrencyManager is necessary because data sources do not necessarily maintain a current-item pointer. 例如,数组和 ArrayList 对象可以是数据源,但它们没有返回当前项的属性。For instance, arrays and ArrayList objects can be data sources, but they do not have a property that returns the current item. 若要获取当前项,请使用 Current 属性。To get the current item, use the Current property.

PropertyManager 还继承自 BindingManagerBase,它用于维护对象的当前属性,而不是数据源中当前对象的属性。The PropertyManager also inherits from the BindingManagerBase, and it is used to maintain the current property of an object, rather than the property of a current object in a data source. 因此,尝试为 PropertyManager 设置 PositionCount 属性不起作用。For this reason, trying to set the Position or Count property for a PropertyManager has no effect.

若要创建 BindingManagerBase,请使用 BindingContext 类,该类根据所管理的数据源返回 CurrencyManagerPropertyManagerTo create a BindingManagerBase, use the BindingContext class, which returns either a CurrencyManager or a PropertyManager, depending on the data source being managed.

鼓励程序员将控件直接绑定到 BindingSource 组件,该组件既充当数据源又充当实际目标数据源的数据连接器。Solutions programmers are encouraged to bind controls directly to a BindingSource component, which acts as both a data source and data connector to the actual target data source. BindingSource 大大简化了简单和复杂的数据绑定,包括管理控件与其目标之间的货币。BindingSource greatly simplifies both simple and complex data binding, including managing currency between the control and its target.

Notes to Implementers

当从 BindingManagerBase继承时,必须重写以下抽象成员: AddNew()CountCancelCurrentEdit()CurrentEndCurrentEdit()GetItemProperties()OnCurrentChanged(EventArgs)PositionRemoveAt(Int32)ResumeBinding()SuspendBinding()UpdateIsBinding()When you inherit from BindingManagerBase, you must override the following abstract members: AddNew(), Count, CancelCurrentEdit(), Current, EndCurrentEdit(), GetItemProperties(), OnCurrentChanged(EventArgs), Position, RemoveAt(Int32), ResumeBinding(), SuspendBinding(), and UpdateIsBinding().

Constructors

BindingManagerBase()

初始化 BindingManagerBase 类的新实例。Initializes a new instance of the BindingManagerBase class.

Fields

onCurrentChangedHandler

指定 CurrentChanged 事件的事件处理程序。Specifies the event handler for the CurrentChanged event.

onPositionChangedHandler

指定 PositionChanged 事件的事件处理程序。Specifies the event handler for the PositionChanged event.

Properties

Bindings

获取所管理绑定的集合。Gets the collection of bindings being managed.

Count

在派生类中重写时,获取 BindingManagerBase 托管的行数。When overridden in a derived class, gets the number of rows managed by the BindingManagerBase.

Current

当在派生类中被重写时,获取当前对象。When overridden in a derived class, gets the current object.

IsBindingSuspended

获取一个值,该值指示绑定是否已挂起。Gets a value indicating whether binding is suspended.

Position

当在派生类中被重写时,获取或设置绑定到该数据源的控件所指向的基础列表中的位置。When overridden in a derived class, gets or sets the position in the underlying list that controls bound to this data source point to.

Methods

AddNew()

当在派生类中被重写时,向基础列表添加一个新项。When overridden in a derived class, adds a new item to the underlying list.

CancelCurrentEdit()

当在派生类中被重写时,取消当前编辑。When overridden in a derived class, cancels the current edit.

EndCurrentEdit()

当在派生类中被重写时,结束当前编辑。When overridden in a derived class, ends the current edit.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetItemProperties()

当在派生类中被重写时,获取绑定的属性说明符集合。When overridden in a derived class, gets the collection of property descriptors for the binding.

GetItemProperties(ArrayList, ArrayList)

使用指定的 ArrayList 获取绑定的属性说明符集合。Gets the collection of property descriptors for the binding using the specified ArrayList.

GetItemProperties(Type, Int32, ArrayList, ArrayList)

获取由此 BindingManagerBase 管理的项的属性列表。Gets the list of properties of the items managed by this BindingManagerBase.

GetListName(ArrayList)

当在派生类中被重写时,获取为绑定提供数据的列表的名称。When overridden in a derived class, gets the name of the list supplying the data for the binding.

GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
OnBindingComplete(BindingCompleteEventArgs)

引发 BindingComplete 事件。Raises the BindingComplete event.

OnCurrentChanged(EventArgs)

引发 CurrentChanged 事件。Raises the CurrentChanged event.

OnCurrentItemChanged(EventArgs)

引发 CurrentItemChanged 事件。Raises the CurrentItemChanged event.

OnDataError(Exception)

引发 DataError 事件。Raises the DataError event.

PullData()

将数据从数据绑定控件拉入到数据源,不返回任何信息。Pulls data from the data-bound control into the data source, returning no information.

PushData()

将数据从数据源推入到数据绑定控件,不返回任何信息。Pushes data from the data source into the data-bound control, returning no information.

RemoveAt(Int32)

当在派生类中被重写时,从基础列表中删除指定索引处的行。When overridden in a derived class, deletes the row at the specified index from the underlying list.

ResumeBinding()

当在派生类中被重写时,恢复数据绑定。When overridden in a derived class, resumes data binding.

SuspendBinding()

当在派生类中被重写时,挂起数据绑定。When overridden in a derived class, suspends data binding.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)
UpdateIsBinding()

当在派生类中被重写时,更新绑定。When overridden in a derived class, updates the binding.

Events

BindingComplete

在数据绑定操作完成时发生。Occurs at the completion of a data-binding operation.

CurrentChanged

在当前绑定项更改时发生。Occurs when the currently bound item changes.

CurrentItemChanged

在当前绑定项的状态更改时发生。Occurs when the state of the currently bound item changes.

DataError

ExceptionBindingManagerBase 处理而未给出任何提示时发生。Occurs when an Exception is silently handled by the BindingManagerBase.

PositionChanged

Position 属性的值更改后发生。Occurs after the value of the Position property has changed.

Applies to

See also