Я пытаюсь отобразить результаты запроса в datagrid WPF. Тип ItemsSource, к которому я привязан, равен IEnumerable<dynamic>
. Поскольку возвращаемые поля не определены до времени выполнения, я не знаю тип данных до тех пор, пока запрос не будет оценен. Каждая "строка" возвращается как ExpandoObject
с динамическими свойствами, представляющими поля.
Я надеялся, что AutoGenerateColumns
(как показано ниже) сможет генерировать столбцы из ExpandoObject
, как это делается со статическим типом, но он не отображается.
<DataGrid AutoGenerateColumns="True" ItemsSource="{Binding Results}"/>
Есть ли способ сделать это декларативно или мне нужно с уверенностью подключиться к некоторым С#?
ИЗМЕНИТЬ
Хорошо, это даст мне правильные столбцы:
// ExpandoObject implements IDictionary<string,object>
IEnumerable<IDictionary<string, object>> rows = dataGrid1.ItemsSource.OfType<IDictionary<string, object>>();
IEnumerable<string> columns = rows.SelectMany(d => d.Keys).Distinct(StringComparer.OrdinalIgnoreCase);
foreach (string s in columns)
dataGrid1.Columns.Add(new DataGridTextColumn { Header = s });
Итак, теперь просто нужно выяснить, как привязать столбцы к значениям IDictionary.