Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: Best way to create bindings to unknown types in MVVM

I am looking for a way to display data in a DataGrid from types that are unknown at compile-time.

I have the following base class

public abstract class Entity
{
    // Some implementation of methods ...
}

In run-time, I load a plug-in DLL and use reflection to get a list of all the types derived from Entity. For example:

public class A : Entity
{
    public LocalAddress Address{ get; set; }
}

public class B : Entity
{
    public Vendor Vendor { get; set; }

    public string Name { get; set; }
}

Then I retreive a list of their instances from DB

public IEnumerable<Entity> Entities { get; set; } // A list of instances of type A for example

Entities is the DataGrid's ItemsSource, But what's the best way I can bind the properties to the DataGrid? Since the properties can be complex, I also need to be able to bind to a specific path, for example Address.HomeNum ...

Clarifications

  1. I only need to show a one grid of a type's instances at a time. The complete scenario is this:

    1. I get a list of types that derive from Entity from the plug-in DLL through reflection
    2. I show their names in a List. (in this example that list will contain A and B
    3. When the user clicks on a specific item, let's say A, I get a list of A instances from DB - so far so good.
    4. I want to display that list of A's instances in a DataGrid.
    5. When the user selects another item from the list (meaning another type, lets say B), I get a list of B's instances from DB and need to display those in the grid and so on ...
  2. The plug-in DLL is a class library with no xamls (also my users are the ones making this plug-ins and I don't want them to have to write DataTemplates for their entities. I also can't make predifned DataTemplates as I don't know the types I'll need to display until run-time. Each type can have different types and amount of properties. All I know in complie-time is that they all derived from Entity.

  3. The grid should also be editable.
like image 561
Omri Btian Avatar asked Oct 30 '13 21:10

Omri Btian


1 Answers

A DataGrid seems inappropriate in this case. If your list was bound to two separate entities, it would break badly.

A better option would potentially be to use some other ItemsControl and set up a DataTemplate for each type of Entity. This would allow you to build custom editors per entity, and have a "list" of them to edit.

If you know the entities will always be of a single type, I'd instead build the collection of that specific type, and bind to it.

like image 114
Reed Copsey Avatar answered Sep 28 '22 04:09

Reed Copsey