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?