2013年8月22日星期四

. NET platform data list control to bind the major principles andcomparative (WebForm, Winform, WPF)

 

talk WebForm:

 

data listing control:

 
  List under WebForm bound controls basic is GridView, DataList, Repeater; course there are other DropDownList, ListBox, etc.  
 

 

their common method of setting data source:

 
  XXX.DataSource = data source.  
 

 

then the format of the data source, whether what's required? The easiest way is just to get hold of, and then wait for it to throw an exception:

 

 

 

from the above errors can see out, basically supports three data source binding way: IListSource, IEnumerable, IDataSource.

 

 

talk Winform

 

data listing control:

 
  List under WinForm bound controls basic is DataGridView, ListView, ComboBox, etc.  
 

 

their common method of setting data source:

 
  XXX.DataSource = data source.  
 

 

the same way and found that it is not life and death thrown exception, through the DataGridView DataSource View Code:

 

 

an insufficient look at what the ComboBox DataSource:

 

 

tips can be obtained from the above information, the data source support: IListSource interface.

 

 

see WPF

 

data listing control:

 
  WPF binding list under control is the basic DataGrid, ListBox, ComboBox, etc.  
 

 

their common method of setting data source:

 
   XXX.    ItemsSource    = data source.     
 

 

for WPF, it is no longer the ItemsSource of type Object, but IEnumerable:

 

  tips can be obtained from the above information, the data source support: IEnumerable interface.  
 


 

three comparison:

 

WebForm: IListSource, IEnumerable, IDataSource.

 

Winform: IListSource

 

WPF: IEnumerable

 

 

in common:

 
  

Because IListSource interface is implemented there must return IList, and IList interface itself has inherited from IEnumerable interface.

  

so the final common is both the IEnumerable interface that is popular support: List collection of such data sources.

 
 

 

different points: (with IDataReader, DataTable, DataSet and other generic collection to illustrate)

 
  

WebForm: Support IDataReader, DataTable, DataSet.

  

Winform: does not support IDataReader, support DataTable, DataSet.

  

WPF: Support IDataReader, does not support the DataTable, DataSet.

 
 

 

there again some story:

 

 

Winform: the shrimp does not support IDataReader?

 
  

For DataGridView control list, in fact, can be directly modified form, and the modified value will be changed directly back to the bound data source.

  

may be out of this nature, the IDataReader for read-only, it is not suitable for reading and writing DataGridView control on dual-demand, so do not support.

 
 

 

Winform the DataTable bound:

 
  

For List , you can easily modify the properties of the collection, and for the DataTable, which is a complex custom class is not something you want to bind the basic properties of the table, but there is another collection of the DataRowCollection.

  

Therefore, designers come up with: PropertyDescriptor and < span style = "line-height: 1.5;"> ICustomTypeDescriptor other attributes describe a set of norms to realize the value of a custom class and bind problem.

 
 

 

WPF: DataTable does not even consider support:

 
  Estimated that the two sides did not communicate well, or is given to the new DataGrid and getting back to basics, removed the written demand, WPF does not even support DataTable, but fortunately there is a property DefaultView, the information can be read like a DataView go tie collection fixed, although many turn step, but will be barely support the binding.  
 
 

 

say MDataTable

 

MDataTable CYQ.Data data layer is a very important framework of a set of classes and achieve a complete mapping between databases, but also shoulder and various UI to deal with, of course, without it I can not to study a variety of binding.

 
  

For custom MDataTable data source bound list controls in this way, a few years ago, I studied for a long time, then the most stable implemented as:

  

public class MDataTable: IDataReader, IEnumerable, IListSource

 
 

 

why MDataTable DataTable in WPF and under the same ineffective?

 
  In fact, here there is a different thing and DataTable is my custom data source inherit IDataReader and IEnumerable, simply, it is supported by the WPF.  
 

 

why binding but the results invalid?

 
  After debugging the code found in the original interface priority issues hot evil, IListSource than IEnumerable to the first level is called, the result left a similar set of rules that DataTable, but the properties of a set of rules to read and WPF is invalid, the result becomes the property binding, rather than rows of data binding.  
 

 

solution:

 
  Since the presence of

IListSource will cause WPF binding fails, then this interface is removed and found WPF to normal.

  

But the problem again, Winform rely on the IListSource interface to bind and remove the IListSource, in winform is not normal.

  

After some pondering, considering DataTable rely DataTableView way to bind WPF, I flashed a trick:

 
 

 

 

Description:

 
  The IListSource interface implementation, assigned to another class MDataTableView, groomed for the inner class description for the user, everything is still business as usual, but the internal method call is in MDataTable.Bind identify current platform, if it is Winform, put the object is changed to: new MDataTableView (ref sourceObje)  
 

 

Summary:

 

although you usually these lists and controls each other, but estimates of binding mechanism, is still relatively small, after all, requires a certain fate.

 

today when the contents of a reference until one day fate comes, when necessary, come review my article also.

 

没有评论:

发表评论