views:

42

answers:

1

Hi I have a datagrid that has a number of datagridtemplate columns that are all identical apart from they each have a different datacontext on the template's stackpanel.

<toolkit:DataGridTemplateColumn Header="Col 1">
                <toolkit:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel DataContext="{Binding Times[0]}">
                        <!-- the structure that I want to extract to a template -->
                        </StackPanel>
                    </DataTemplate>
                </toolkit:DataGridTemplateColumn.CellTemplate>
</toolkit:DataGridTemplateColumn>

<toolkit:DataGridTemplateColumn Header="Col 2">
                <toolkit:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel DataContext="{Binding Times[1]}">
                        <!-- the same structure here -->
                        </StackPanel>
                    </DataTemplate>
                </toolkit:DataGridTemplateColumn.CellTemplate>
</toolkit:DataGridTemplateColumn>

I want to have each column use a specific itemtemplate (like I've done with a listbox) but can't seem to see how unless I'm missing something.

A: 

You could use a ContentPresenter to instantiate a DataTemplate for each column:

<toolkit:DataGrid.Resources>
    <DataTemplate x:Key="ColumnTemplate">
        <StackPanel>
            <!-- the structure that I want to extract to a template -->
        </StackPanel>
    </DataTemplate>
</toolkit:DataGrid.Resources>
<toolkit:DataGrid.Columns>
    <toolkit:DataGridTemplateColumn Header="Col 1">
        <toolkit:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <ContentPresenter ContentTemplate="{StaticResource ColumnTemplate}" Content="{Binding Times[0]}"/>
            </DataTemplate>
        </toolkit:DataGridTemplateColumn.CellTemplate>
    </toolkit:DataGridTemplateColumn>
    <toolkit:DataGridTemplateColumn Header="Col 2">
        <toolkit:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <ContentPresenter ContentTemplate="{StaticResource ColumnTemplate}" Content="{Binding Times[1]}"/>
            </DataTemplate>
        </toolkit:DataGridTemplateColumn.CellTemplate>
    </toolkit:DataGridTemplateColumn>
</toolkit:DataGrid.Columns>

If the elements of Times are all the same type, you could also do <DataTemplate DataType={x:Type YourType}> and then you wouldn't need to specify ContentTemplate="{StaticResource ColumnTemplate}" on each column.

Quartermeister
Thanks that's what I was looking for. I had the template created but didn't know about the ContentPresenter for displaying it in the column.One last thing, do you know how I could alternate the background color of these templated columns?Thanks again.
obaylis
@obaylis: I think you can just set the AlternatingRowBackground property on the DataGrid. If you need something more complicated, you can set AlternationCount and then use AlternationIndex. Here is a blog post that describes ways to style a DataGrid: http://blogs.msdn.com/b/jaimer/archive/2009/01/20/styling-microsoft-s-wpf-datagrid.aspx
Quartermeister