开发者

WPF - DataGrid Custom Binding - List of Custom Objects with dynamic number of properties - C#

Using Linq pad I created a view on data in the database which I now hope to replicate in a WPF Application.

I took advantage of the Linq Dump() method. By implementing ICustomMemberProvider I could provide the column headers, types and values which I wanted to be output. The three methods I needed to implement were;

   public IEnumerable<string> GetNames() 
   public IEnumerable<Type> GetTypes()
   public IEnumerable<object> GetValues()

This was a simple, quick and clean way of describing what the Dump()' for single or multiple rows should be.

For the life of me I cannot find anything as straight forward in WPF. I have a dynamic (per run not per row) number of Columns and so I can't hard code Column titles and binding paths, there may be 5 columns and there may be 20.

I was pointed towards ICustomTypeDescriptor but I need a concrete example of how that would work as there are so many methods in that interface.

I'm really hoping there's something simpler that I've missed which will allow me to implement dynamically what the rows and columns should contain given an IEnumerable of my custom class.

Any links to a tutorial or overview of how this is meant to work would be greatly appreciated. I have been surprised by the lack of documentation I've found so I must be using the wrong terms.

For clarity the source of a single row is an instance of a class like this;

public class CustomDatum 
{
   public string ID {get; private set;}
   public string Location {get; private set;}

   public IE开发者_如何学编程numerable<Attributes> attributes {get; private set;}

   public class Attribute 
   {
       public string Name {get; private set;}
       public string Value {get; private set;}

       public override ToString()
       {
           ....
       }
   }
}

I want to display the ID, Location and all attributes in a single Row, I have an IEnumerable<CustomDatum> to bind to. The actual class is a lot more complex than this example naturally.

Thanks!


I'm pretty sure you can use a DataGridView and set its AutoGenerateColumns to true.

example of ICustomTypeDescriptor

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜