In the project I’m currently working on, I don’t have plain CLR objects to bind with my datagrid. Normally, I would use an ObservableCollection<MyObject>, but now I can’t and instead I must use a custom object (which behaves like a DataTable but with custom logic).
My question is : How can I make a proxy/adapter object that will “translate” every binding operations on items in cells of my datagrid to my custom logic object?
Remember that:
- My object is not a Collection, so I don’t really have Items.
- The “columns” of the virtual items might change.
- Somewhere in the application I might change one value in that object and that the datagrid must reflect changes
Right now we are recreating a DataTable from the custom object to get his DataView to display in a datagrid and we intercept editing commands with events (which are ugly) and then reload everything from scratch each time when change a cell value.
I searched for hints about how to accomplish this but the things I found are :
- IItemProperties interface (Might be interesting to have dynamic columns but I don’t have a collection)
- DataSourceProvider class (It appears to be a wrapper for ItemsSource, but I don’t see what I have to return in the Data property)
- INotifyPropertyChanged interface (To tell a property changed, but I don’t have items or at least items don’t have properties)
- ICustomTypeDescriptor interface (Ok but once I returned the properties with GetProperties() how the datagrid will try to modify the inexistent property?)
As you can see, it’s confusing about choosing a way to implement a good proxy.
Finally I found a solution, I used the second approach in this web site : and made an object that inherits from IBindingList and ITypedList to fake that the item is a list, I used also ICustomTypeDescriptor on each fake rows and called the master table object.