I have the following scenario, I want to create a DataGrid and then populate the contents at runtime.
The issue I am having is that because I do not know what the fields are until I am creating the grid I do not know how to set the item source correctly. As you can see in the following code I am adding the Field names as columns, I am then looping through my items and at this point I want to create an anonymous type for each item where I am setting a property (called its field name) to the field names value
foreach (string fieldName in listViewFieldCollection)
{
DataGridTextColumn newColumn = new DataGridTextColumn
{
Header = fieldName,
Binding = new Binding(fieldName)
};
dataGrid.Columns.Add(newColumn);
}
List<object> list = new List<object>();
foreach (ListItem listItem in listItems)
{
foreach (string fieldName in listViewFieldCollection)
{
list.Add(new
{
// I want to be able to dynamically build Anonymous type properties here
fieldName = listItem[fieldName]
});
}
}
dataGrid.ItemsSource = list;
for example. If I have the Fields called 'Title' and 'Link' then I want the list.Add(new to behave as
list.Add(new
{
Title = listItem["Title"],
Link = listItem["Link"]
});
but if the fields are 'Manager', 'Title' and 'Salary' it should perform like
list.Add(new
{
Manager = listItem["Manager"],
Title = listItem["Title"],
Salary = listItem["Salary"],
});