I want to add a row using data from a ExpandoObject
, which is similar to a Dictionary<string, object>
. The string
is the header of the column and the object
's value is value of the column.
Everytime, when I get new data I'm creating a new GridView
, because the number of columns can be different. In the List
myItems are all rows Dictionary<string, object>
, that I want to show in my view.
This is how I add the columns to my view:
List<Column> columns = new List<Column>();
myItemValues = (IDictionary<string, object>)myItems[0];
// Key is the column, value is the value
foreach (var pair in myItemValues)
{
Column column = new Column();
column.Title = pair.Key;
column.SourceField = pair.Key;
columns.Add(column);
}
view.Columns.Clear();
foreach (var column in columns)
{
Binding binding = new Binding(column.SourceField);
if (column.SourceField == "Icon")
{
view.Columns.Add(new GridViewColumn
{
Header = column.Title,
DisplayMemberBinding = binding,
CellTemplate = new DataTemplate(typeof(Image))
});
}
else
{
view.Columns.Add(new GridViewColumn { Header = column.Title, DisplayMemberBinding = binding });
}
}
Direct after this I try to add the rows:
foreach (dynamic item in myItems)
{
this.listView.Items.Add(item);
}
I tryed to modify this solution for an other purpose. This solution works very well, if I only want to add values of the type string
, but now I also want to display an image
in the gridview, but if I add one to my gridview, it shows me just:
"System.Windows.Controls.Image"
Now I want to know, if I can modify my code so as I can display any type (or at least images
and strings
) in a gridview or do I have to use a completly new way and would be the way?
EDIT: In the previous approaches, it was said, that I need to create a new DataTemplate
to show an image, but none of the solutions(Solution 1, Solution 2) I found is working for me.