I came across this code sample at TechNet witch shows how to bind a single column header title and its corresponding columns data :
<DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>
<TextBlock Text="{Binding DataContext.HeaderNameText, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding}" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
and the DataContext of the DataGrid is set like so :
Data data = new Data();
data.HeaderNameText = "Header2";
data.Items = new List<string>() { "Item1", "Item2" };
DataContext = data;
the Data class structure is as follow :
public class Data
{
public string HeaderNameText { get; set; }
public List<string> Items { get; set; }
}
my question is how to Bind the DataGrid ItemSource to multiple columns data :
List<Data> data=new List<Data>();
i am expecting to get a column for every data element in the above list, so how to achieve that?
Dataobjects then you'll have multipleItemsas well which makes little sense to me. Wouldn't it be more sensible to have a list of objects which have the value and column header set individually?ItemsSource? Or simply that the number of columns in theItemsSourceisn't known ahead of time? If the former, you're going to have to manage the set of columns as the rows/records come in.