How can/could/might you bind WPF DataGrid to a List of objects each with some values in a Dictionary

Posted by panamack on Stack Overflow See other posts from Stack Overflow or by panamack
Published on 2010-04-06T16:49:40Z Indexed on 2010/04/16 22:03 UTC
Read the original article Hit count: 473

Filed under:
|

major edit of a tumbleweed, may remain a tumbleweed...

If I have a list of Customers and the data for each Customer is contained in a Dictionary how can I bind the list to the DataGrid so that each string key is a column?

Edit: N.B. I know it's not a nice way to design a Customer class.

e.g.

public class Customer{

    public int Id{get;set;}

    private Dictionary<string,string> values;

    public Dictionary<string,string> Values{get {return values;}}

    public Customer(int id){
         this.Id = id;
         values["Name"] = "Peter";
         values["Age"] = 129.ToString();
         values["HairColour"] = "See through!";
    }
}

... later that day...

var Customers = new List<Customer>(){
    new Customer(1),
    new Customer(2), 
    new Customer(3)
};

... and then...

<DataGrid ItemsSource={Binding Path=Customers}/>

... desired result.

Id | Name | Age |  HairColour
________________________
1  | Peter| 129 | See through!
________________________

2  | Peter| 129 | See through!
________________________

3  | Peter| 129 | See through!
________________________

© Stack Overflow or respective owner

Related posts about wpf

Related posts about wpftoolkit