I want to add a string using data from ExpandoObject , which is similar to Dictionary<string, object> . string is the column heading, and object is the column value. Every time I get new data, I create a new GridView , because the number of columns may be different. In List myItems are all the Dictionary<string, object> lines that I want to show in my view.
This is how I add 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 }); } }
Right after that I try to add the lines:
foreach (dynamic item in myItems) { this.listView.Items.Add(item); }
I tried to change this solution for other purposes. This solution works very well if I only want to add values โโof type string , but now I also want to display image in gridview, but if I add it to gridview, it will show me simply:
"System.Windows.Controls.Image"
Now I want to know if I can change my code so that I can display any type (or at least images and strings ) in gridview, or do I need to use a completely new way and path?
EDIT: In previous approaches, it was said that I needed to create a new DataTemplate to show the image, but none of the solutions ( Decision 1 , Decision 2 ) I found a job for me.
c # image wpf gridview
daniel59
source share