I want to bind a collection of data as different columns in a DataGrid
. The normal way we bind data is bind property. But now my scenario is the properties is a dynamic collection which might be different from different object instances. For each object I need dynamically create a DataGrid
.
Here I give a simplified live example which is abstracted from my scenario :
public class Company
{
...
public string CompanyName { set; get; }
public ObservableCollection<CompanyProperty> data;
}
public class CompanyProperty
{
public string PropertyName { get; set; }
public string Value { get; set; }
}
In my main class, I have a ObservableCollection of Company:
class Window
{
private ObservableCollection<Company> _list;
private void CreateData()
{
Company tmp = new Company("Siemens");
tmp.data.Add(new CompanyProperty() { PropertyName = "Country", Value = "Germany" });
tmp.data.Add(new CompanyProperty() { PropertyName = "Staffs", Value = "400,000" });
_list.Add(tmp);
tmp = new Company("ABB");
tmp.data.Add(new CompanyProperty() { PropertyName = "Country", Value = "Swiss" });
tmp.data.Add(new CompanyProperty() { PropertyName = "Staffs", Value = "100,000" });
_list.Add(tmp);
}
}
Now I want to bind my data to a DataGrid
which will be looked like 3 columns(CompanyName, Country, Staffs) and 2 rows(Siemens, ABB).
What I'm struggling with is how to bind the collection of company properties to the different columns in my DataGrid
since the "Country" and "Staffs" is not properties of "Company" class but only values in an ObservableCollection
member.