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?

3
  • 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? Commented Nov 6, 2014 at 13:58
  • the number of columns is a changing factor, that can't be set statically(the header title also) ! Commented Nov 6, 2014 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. Commented Nov 6, 2014 at 16:29

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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!
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 (keywords are wpf datagrid column dynamically).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.