Binding class

       It represents the simple binding between the property value of an object and a property value of the control.

 

 

Remark

Use Binding class to create and maintain a simple binding between the property or properties of a control list of objects and an object of the current properties of the object.

As an example of the first case, you can bind the Text property of the TextBox control to the "Customer" (Customer) object "name" (FirstName) property. As an example of the second case, you can TextBox control's Text property is bound to include the customer's DataTable "name" (FirstName) property.

Binding class also allows you to format the values displayed by the Format event, as well as formatting retrieved by Parse event value.

When a Binding Constructor Binding instance, you must specify three: the name of the control property to bind to data sources and navigation path that resolves to a list or data source properties. The navigation path is also used to create the object BindingMemberInfo.

First, you must specify the name you want to bind the data to control properties thereon. For example, to TextBox display data control, specify the Text property. Next, the following may be any instance of a class specified as the data source:

description Examples
IBindingList or achieve any kind of ITypedList. Including: DataSet, DataTable, DataView or DataViewManager. C # example:

DataSet ds = new DataSet("myDataSet");

Implements IList to create any class of object index collection. We must create a Binding to create and populate the collection before the object. All objects in the list must be the same type; otherwise it will throw an exception. C # example:

ArrayList ar1 = new ArrayList;

Customer1 cust1 = new Customer("Louis");

ar1.Add(cust1);

Strongly typed IList strongly typed object C # example:

Customer [] custList = new Customer[3];

Third, you must specify the navigation path, which can be an empty string ( ""), or a single attribute names separated by periods name hierarchy. If the navigation path to an empty string, the ToString method is called on the underlying data source object.

If the data source may comprise a plurality of objects DataColumn the DataTable , you must use the navigation path resolution for a particular column.

Note that    when the data source is the DataSet , a DataViewManager or DataTable when actually bind to the DataView . Thus, the binding line actually DataRowView object.

When the data set comprising a plurality of source DataTable objects (e.g., objects DataSet or DataViewManager ), the required period-separated navigation route. When bound to its target property returns a reference to other objects (e.g., having the properties of other classes returned object classes) may be used to navigate the path separated by periods. For example, the following description are valid navigation route data fields:

  • “Size.Height”
  • “Suppliers.CompanyName”
  • “Regions.regionsToCustomers.CustomerFirstName”
  • “Regions.regionsToCustomers.customersToOrders.ordersToDetails.Quantity”

Each member of the return path may be resolved to a single value (e.g., an integer) properties, return to the list (such as an array of strings) values. Although each member of the path can be a list or property, but in the end the members must resolve property. Each member on the establishment of a member of the former: "Size.Height" to resolve the current Size of Height; "Regions.regionsToCustomers.CustomerFirstName" resolved to the current name of the customer, where the customer is a customer of the current area.

By DataRelation DataSet a the DataTable and second DataTable link return a list of values. If DataSet contains DataRelation object, the data may be designated as members of the TableName, followed RelationName, then ColumnName. For example, if the name "Suppliers" of the DataTable contains a file named "suppliers2products" the DataRelation , then the data member may be "Suppliers.suppliers2products.ProductName".

Data source may consist of a group of related classes. For example, the solar system is contemplated to be a set of class catalog. System class name attribute contains the name of the Stars, which returns the collection of objects star. Each "star" (Star) object has the "Name" (Name) and "quality" (Mass) attribute, and "planet" return "planet" (Planet) is a collection of objects (Planets) property. In this system, each planet also has a "quality" (Mass) and "name" (Name) property. Each "planet" (Planet) objects in addition to the "Moon" (Moons) property, which returns a collection (Moon) object "Moon", each "moon" objects also have "name" (Name) and "quality" ( Mass) attribute. If the "System" (System) object as the data source, you can specify any of the following data members:

  • “Stars.Name”
  • “Stars.Mass”
  • “Stars.Planets.Name”
  • “Stars.Planets.Mass”
  • “Stars.Planets.Moons.Name”
  • “Stars.Planets.Moons.Mass”

Simple controls can bind to ControlBindingsCollection in Binding collection object features, you can access the collection through the DataBindings property of the control. Adding to the collection by calling the Add method Binding objects, which will bind the property of the control to the properties of an object (or bound to a list of the current properties of the object).

You can simply bind to any object derived from the System.Windows.Forms.Control class, Windows controls such as the following:

  • Button
  • CheckBox
  • CheckedListBox
  • ComboBox
  • DateTimePicker
  • DomainUpDown
  • GroupBox
  • HScrollBar
  • Label
  • LinkLabel
  • ListBox
  • ListView
  • MonthCalendar
  • NumericUpDown
  • PictureBox
  • ProgressBar
  • RadioButton
  • RichTextBox
  • ScrollBar
  • StatusBar
  • TextBox
  • TreeView
  • VScrollBar
Note that    only the ComboBox , CheckedListBox and the ListBox the SelectedValue property of the control is simple binding.

BindingManagerBase class is the managing specific data sources and members of the Binding abstract class object. From BindingManagerBase derived class is a class CurrencyManager and PropertyManager classes. How to manage Binding depends Binding is a list of binding or binding properties. For example, if a list binding, you can use BindingManagerBase objects specified list Position; therefore Position determine which one (all items in the list) actually bound to the control. To return appropriate the BindingManagerBase , please use the BindingContext.

To new row to bind to the same DataSource is a set of controls to add, please use the BindingManagerBase AddNew method of the class. Use BindingContext class Item property returns appropriate the CurrencyManager . To avoid adding a new row, use CancelCurrentEdit method.

Reproduced in: https: //www.cnblogs.com/yitian/archive/2009/02/04/1383685.html

Guess you like

Origin blog.csdn.net/weixin_34010949/article/details/93710352