4

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?

SamTh3D3v
  • 9,854
  • 3
  • 31
  • 47
  • If you're going to use a list of `Data` objects then you'll have multiple `Items` as 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? – Jerrington Nov 06 '14 at 13:58
  • the number of columns is a changing factor, that can't be set statically(the header title also) ! – SamTh3D3v Nov 06 '14 at 14:01
  • Are you saying the number of columns varies between rows in the same `ItemsSource`? Or simply that the number of columns in the `ItemsSource` isn'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. – Mike Strobel Nov 06 '14 at 16:29

1 Answers1

1

Given approach defines just one column

<DataGrid>
    <DataGrid.Columns>
        <DataGridTemplateColumn ...>
    </DataGrid.Columns>
</DataGrid>

For multiple columns you'll have to define it multiple times

<DataGrid>
    <DataGrid.Columns>
        <DataGridTemplateColumn ...>
        <DataGridTemplateColumn ...>
        <DataGridTemplateColumn ...>
    </DataGrid.Columns>
</DataGrid>

If you define columns like this

public class Data
{
    public string[] HeaderNameText { get; set; } // array, list, etc.
    public List<string> Items { get; set; }
}

then binding will be something like

{Binding DataContext.HeaderNameText[0] ...}
{Binding DataContext.HeaderNameText[1] ...}

etc.

Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • The number of columns could change depends on the data.Count(), i don't want to define them statically in the xaml, but i need to define a data template that will apply on each columns! – SamTh3D3v Nov 06 '14 at 13:28
  • 1
    Then given approach won't work. You can either use `AutoGenerateColumns = true` and supply column template (I seen somewhere here such approach) or generate `Columns` dynamically. Both required some code behind. Use search, to example [here](http://stackoverflow.com/q/1983033/1997232) (keywords are `wpf datagrid column dynamically`). – Sinatr Nov 06 '14 at 13:32