Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><a href="http://www.codeproject.com/Articles/36497/Binding-a-Silverlight-DataGrid-to-dynamic-data-via" rel="nofollow">This tutorial</a> suggests an approach that looks a lot simpler than emitting dynamic classes. You basically leverage the Converter property to bind to an indexed value on the row of data.</p> <p>I just tried this approach, and it seems to work great. Here's some code:</p> <pre><code>/// &lt;summary&gt; /// Allows us to bind columns to dictionary entries. /// &lt;/summary&gt; public class DictionaryConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var dict = (IDictionary) value; return dict[parameter]; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } </code></pre> <p>Usage:</p> <pre><code>List&lt;IDictionary&lt;string, object&gt;&gt; dicts = GetDictionaryData(); foreach (var series in _trendChart.Series) { _dataGrid.Columns.Add( new DataGridTextColumn{ Header = series.Name, Binding = new Binding{ Converter = new DictionaryConverter(), ConverterParameter = series.Name } }); } _dataGrid.ItemsSource = dicts; </code></pre> <h3>Update</h3> <p><a href="http://www.scottlogic.co.uk/blog/colin/2010/03/binding-a-silverlight-3-datagrid-to-dynamic-data-via-idictionary-updated/" rel="nofollow">This article</a> includes a much more complete set of code that makes it possible to add sorting and other advanced features.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload