Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
40
Data binding a heterogeneous collection and providing a flattened/single view within the grid
posted

Hi,

I've been attempting to bind a heterogeneous collection (it's a class hierarchy with only two levels) to the XamDataGrid using an ObjectDataProvider instance.

While this works fine, the grid generates a column header for each row, as the types differ. What I would like to do is generate a single header that contains required columns for all data types within the collection and have empty or greyed out cells for the columns that don't apply to the type in question.

For example:

interface IVehicle
{
  string Manufacturer { get; }
  string Model { get; }
}

interface ICar : IVehicle
{
  int Doors { get; }
}

interface IBoat : IVehicle
{
  int NumEngines { get; }
}

Displayed as:

Handling the AssigningFieldLayoutToItem event and specifying the same FieldLayout would be ideal, if it were not for the fact that all fields must exist on every object (the docs mention an exception is thrown if this isn't the case).

While I could provide a view model which wraps around every IVehicle derived type and exposes all properties, this is rather time consuming and makes maintenance difficult. Unlike the vehicle example above, my model contains 7 dervived types all of which have between 5 and 10 unique properties. That, and the properties will (and are) changing during development.

Is there a way to attempt to assign a single FieldLayout and prevent exceptions from being thrown?

Alternative suggestions welcome.

Thanks in advance,

Andrew

Parents
No Data
Reply
  • 69686
    posted

    Hello Andrew,

    I am not sure if you can trick the XamDataGrid into displaying different classes with one field layout using bound fields. It will generate a field layout for all the public properties of that class. What you could use is Unboundfields (and set the binding path) only and thus you would choose what fields you want or not in your layout. However, please note that this will throw exceptions in the output, as certain cells will not be able to bind to the underlying properties which does not exist in that class.

Children
No Data