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.
This is a complex area; the following is a breakdown from winforms binding, but I understand that WPF binding isn’t that different;
IListSource– and if so useGetList()to get the actual binding (this is how a data-table becomes a data-view for binding purposes)ITypedList; this acts as a flexible way of obtaining pseudo-properties (GetItemProperties()) represented by the model; data-view implementsITypedList, creating pseudo-properties per instancepublic SomeType this[int index] {get;}– note that most collections will satisfy thisTin eitherIList<T>orIEnumerable<T>if the object implements those interfacesGetType()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 (viaTypeDescriptionProvider) to supply properties for a type (this can be added at runtime, which can be very convenient)TypeDescriptor.GetProperties(obj)– in addition to reflection andTypeDescriptionProvider, this also has support forICustomTypeDescriptorwhich can be implemented by an individual object to supply custom properties at runtime (very similar toTypeDescriptionProvider, 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
IListSourceandITypedListprocessing is identical. From memory, most (all?) of the winforms strategies will work on WPF – so it could be that and changes are additional hooks.