Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
145
Dynamic Columns using ExpandoObject
posted

I'm trying to create a datagrid with dynamic columns. The datasource is a Collection of ExpandoObject(s). They'all have the same properties.

I'm able to get this using the WPF native data grid as listed below

XAML

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Results}"/>

Code

IEnumerable<IDictionary<string, object>> rows = dataGrid1.ItemsSource.OfType<IDictionary<string, object>>();
IEnumerable<string> columns = rows.SelectMany(d => d.Keys).Distinct(StringComparer.OrdinalIgnoreCase);
foreach (string text in columns)
{
   
// now set up a column and binding for each property
   
var column = new DataGridTextColumn
   
{
       
Header = text,
       
Binding = new Binding(text)
   
};

    dataGrid1
.Columns.Add(column);
}

I'm trying to get the same behavior using XamDataGrid. I'm trying to dynamically add fields to a fieldlayout. This doesnt seem to work. Every ExpandoObject gets rendered as a Key Value Pair (I guess since it implements IDictionary<object,string>)

Is there a way I can get XamDataGrid to layout each property in the ExpandoObject as a field?

Thanks and appreciate your help