Привет у меня есть datagrid, который имеет много datagridtemplate столбцов, которые все идентичны кроме, у каждого из них есть другой datacontext на 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>
Я хочу иметь каждое использование столбца определенный itemtemplate (как, я сделал с полем списка), но, может казаться, не видит как, если я не пропускаю что-то.
Вы могли бы использовать ContentPresenter для инстанцирования DataTemplate для каждого столбца:
<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>
Если элементы Times все одного типа, вы также можете сделать
и тогда вам не нужно будет указывать ContentTemplate="{StaticResource ColumnTemplate}"
на каждой колонке.