5

I am making a WPF user control and I want similar behavior as DataGrid control in sense of binding. My question is: How does DataGrid know how to bind to any collection of type IEnumerable? For example: You can pass a DataView as ItemsSource, and you can also pass any object collection. How DataGrid decides whether to bind to a column of DataView, or to a property of object only by looking at this:

<DataGridTextColumn Binding="{Binding **Path=SomePropertyOrColumn**}"/>

Thanks in advance.

Vale
  • 3,258
  • 2
  • 27
  • 43
  • Check this http://stackoverflow.com/questions/5488014/in-wpf-are-binding-path-and-binding-really-equal, http://stackoverflow.com/questions/3504480/when-to-use-path-in-wpf-binding and http://stackoverflow.com/questions/4742449/new-to-wpf-data-binding – Sandeep G B Sep 15 '11 at 06:42
  • @Sandeep G B That was not my question. I know how to do binding, I want to know how does DataGrid do it internaly with any object. – Vale Sep 15 '11 at 06:45

2 Answers2

3

This is a complex area; the following is a breakdown from winforms binding, but I understand that WPF binding isn't that different;

  • given an object, the first thing binding code will do is look for IListSource - and if so use GetList() to get the actual binding (this is how a data-table becomes a data-view for binding purposes)
  • after that, the source is typically checked for ITypedList; this acts as a flexible way of obtaining pseudo-properties (GetItemProperties()) represented by the model; data-view implements ITypedList, creating pseudo-properties per instance
  • otherwise, it might be possible to identify an obvious type for the data, which can mean:
    • looking for an indexer of the form public SomeType this[int index] {get;} - note that most collections will satisfy this
    • (not done by winforms, but might be for WPF) resolving via reflection the T in either IList<T> or IEnumerable<T> if the object implements those interfaces
    • taking the first object (if any) from the sorce, and using GetType()
  • once a type is known, TypeDescriptor.GetProperties(type) can be used to obtain properties; in many cases this will be via reflection, but it is also possible to add an indirection layer (via TypeDescriptionProvider) to supply properties for a type (this can be added at runtime, which can be very convenient)
  • in the case of individual binding (not list binding), there is also TypeDescriptor.GetProperties(obj) - in addition to reflection and TypeDescriptionProvider, this also has support for ICustomTypeDescriptor which can be implemented by an individual object to supply custom properties at runtime (very similar to TypeDescriptionProvider, but with the individual object taking responsibility for the properties)

I don't know exactly how much of this applies to WPF binding, but I'm pretty sure the IListSource and ITypedList processing is identical. From memory, most (all?) of the winforms strategies will work on WPF - so it could be that and changes are additional hooks.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
0

I think that:

you can get Type of element of its Collection, this type Type have GetProperties() method, which returns all public properties available in its type, and, after its know all public properties, it can bind with them.

stukselbax
  • 5,855
  • 3
  • 32
  • 54
  • Yes, but how does it work with DataView? It is a collection of DataRowView objects, and those objects do not have Column property, and they are still binded to columns. – Vale Sep 15 '11 at 06:52
  • I think that this article should help [MSDN](http://msdn.microsoft.com/en-US/library/bb669099(v=VS.100).aspx). Field(string) - may be you search this? – stukselbax Sep 15 '11 at 07:22