0

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 IEnumerable<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!

1 Answer 1

1

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

example of ICustomTypeDescriptor

Sign up to request clarification or add additional context in comments.

5 Comments

It won't AutoGenerateColumns as my columns are in an Enumerable inside the object
doh, reading ftl. I missed the part where you said that ICustomTypeDescriptor looks complicated, which means you already looked at AutoGenerateColumns. Still, it seems to me that you only need to implement GetProperties() to make it work. Example linked in main answer.
Fantastic, thanks! Would you agree that it's still fairly complex though? I think perhaps I should be populating the grid programatically?
it would be nice to make it simpler than it is, but as it stands, I don't think it's VERY complex. If you populate the grid programatically, you introduce a coupling between the UI and the ViewModel (if that's what you're using), which runs against the typical goals of WPF programming (but might be appropriate for your needs).
Rather than implement ICustomTypeDescriptor, extending CustomTypeDescriptor allows you to override only the methods you need (GetProperties). This is tidier but has obvious inheritence implications.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.