How can / could / you could bind WPF DataGrid to a list of objects, each of which has some values ​​in the <string, string> dictionary?

big change tumbleweed, can remain tumbleweed ...

If I have a list of Clients and the data for each Client is contained in a dictionary, how can I bind this list to a DataGrid so that each row key is a column?

Edit: NB I know this is not a good way to create a Customer class.

eg.

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}/> 

... the desired result.

 Id | Name | Age | HairColour ________________________ 1 | Peter| 129 | See through! ________________________ 2 | Peter| 129 | See through! ________________________ 3 | Peter| 129 | See through! ________________________ 
0
source share
2 answers

In the absence of other suggestions, this article is the best answer I can come up with. In his article, Miron works with XML data from a web service and converts it to a generated Reflection type for data binding.

I can follow his approach and create a type that uses my dictionary keys, not xml nodes, as a source of properties of the generated type. For the rest of the application I'm building, it seems pretty massive, but at least I will learn about the Reflection API.

If someone wants to comment or can provide me with a better solution, I would appreciate it.


Or, easy road ...

 public partial class Window2 : Window { public Window2() { InitializeComponent(); var a1 = new A(); a1["Name"] = "Jack"; a1["Age"] = "9"; var a2 = new A(); a2["Name"] = "Jill"; a2["Age"] = "7"; List<A> items = new List<A>() { a1, a2 }; this.DataBoundItems = items; dg.DataContext = this; } public List<A> DataBoundItems { get; set; } private void dg_DataContextChanged( object sender, DependencyPropertyChangedEventArgs e) { foreach (string key in DataBoundItems[0].Values.Keys) { var col = new DataGridTextColumn(); col.Header = key; // bind to the indexer on the class col.Binding = new Binding("[" + key + "]"); dg.Columns.Add(col); } } } public class A { private Dictionary<string, string> values = new Dictionary<string, string>(); public string this[string index] { get { return values[index]; } set { values[index] = value; } } public Dictionary<string, string> Values { get { return aValues; } } } 
+1
source

I do not get the design of your Customer class. It will be much safer to class customers as follows. This would also make binding easier.

 public class Customer { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public HairColor HairColor { get; set; } } public enum HairColor { SeeThrough, Black, Brown, Blond } 

If the reason is that it is because that is how you get it from the database, then consider your class as Model and my class as ViewModel with the corresponding conversion done in the ViewModel class.

0
source

Source: https://habr.com/ru/post/1308081/


All Articles