How to use Dictionary to provide data (which are defined in runtime) to DataGrid (xaml) in SL4
I am looking for information on how to provide data to DataGrid using Dictionary. My use case is: I have WebService that provides me a table with the content (and column names) I get know in runtime.
My current implementation of a data tuple is:
public class GridTuple : Dictionary<string, string>
{
public GridTuple(IDictionary<string, string> cv, int id) : base(cv)
{
TupleId = id;
}
public int TupleId { set; get; }
public override string ToString()
{
string result = TupleId + " ";
for (int i = 0; i < ColumnNames.Names.Count; i++)
{
result += ColumnNames.Names[i] + " " + this[ColumnNames.Names[i]] + " ";
}
return result;
}
}
...and implementation for DataGrid in xaml is:
<sdk:DataGrid AutoGenerateColumns="True" HeadersVisibility="All" x:Name="dataGrid"
HorizontalAlignment="Left" VerticalAlignment="Top"
ItemsSource="{Binding ListOfGridTuples}" MouseLeftButtonUp="dataGrid_MouseLeftButtonUp"
SelectionMode="Single" Grid.Column="1" >
<sdk:DataGrid.Columns>
<!-- ValueForColumn1 is a key in my GridTuple -->
<sdk:D开发者_如何学JAVAataGridTextColumn Binding="{Binding [ValueForColumn1]}" />
</sdk:DataGrid.Columns>
</sdk:DataGrid>
...
Solved. I have found this post - http://blog.bodurov.com/How-to-Bind-Silverlight-DataGrid-From-IEnumerable-of-IDictionary/. I have created Columns with Bindings following this post. Only change I made was to put a ids of columns in Bindngs in '[' ']' because SL4 provides support for dictionaries in xaml: new DataGridTextColumn { Header = "ID", Binding = new Binding("[ID]") });
精彩评论