At the moment it is impossible to reuse the same DataTemplate for multiple DataTemplateColumns because the binding is part of the template and cannot be defined on the DataGridTemplateColumn definition itself.
Here's a post I put on the Silverlight forum (http://forums.silverlight.net/p/245344/615661.aspx/1?p=True&t=634611643101728290) which explains a bit more:
Hi,
I realise that this has been asked before in the forums, but I've yet to see a good answer, I guess it's either not possible or maybe so obvious I'm missing the answer!?
I'm using Silverlight 5 and I have a datagrid where each row represents a year and each column a month. For each month I am showing a total percentage value which is relevant to my application.
I have a collection of monthly total objects where the class looks something like this:
public class MonthlyTotals
{
public int Year { get; set; }
public decimal Jan { get; set; }
public decimal Feb { get; set; }
public decimal Mar { get; set; }
public decimal Apr { get; set; }
public decimal May { get; set; }
public decimal Jun { get; set; }
public decimal Jul { get; set; }
public decimal Aug { get; set; }
public decimal Sep { get; set; }
public decimal Oct { get; set; }
public decimal Nov { get; set; }
public decimal Dec { get; set; }
public decimal YearlyTotal { get; set; }
}
Each month column in the data grid is defined with a datatemplate like this:
<sdk:DataGridTemplateColumn Width="Auto"
CanUserSort="False"
Header="Jan">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Right"
VerticalAlignment="Center"
Foreground="{Binding Jan,
Converter={StaticResource colourConverter}}"
Text="{Binding Jan,
StringFormat='p2'}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn Width="Auto"
CanUserSort="False"
Header="Feb">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Right"
VerticalAlignment="Center"
Foreground="{Binding Feb,
Converter={StaticResource colourConverter}}"
Text="{Binding Feb,
StringFormat='p2'}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn Width="Auto"
CanUserSort="False"
Header="Mar">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Right"
VerticalAlignment="Center"
Foreground="{Binding Mar,
Converter={StaticResource colourConverter}}"
Text="{Binding Mar,
StringFormat='p2'}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
This is repeated for all 12 months plus the yearly total.
My question is, given that the datatemplate is the same for every column, how do I define it once and reuse it. I realise that I can define it in the static resources but I don't understand how to do the binding given that each column is bound to a different field?
I want to do something like this:
In the datagrid:
<sdk:DataGridTemplateColumn Width="Auto"
CanUserSort="False"
Header="Jan" CellTemplate={StaticResource columnTemplate} />
In the static resources I would have something like this:
<UserControl.Resources>
<DataTemplate x:Key="columnTemplate">
<TextBlock HorizontalAlignment="Right"
VerticalAlignment="Center"
Foreground="{Binding ???,
Converter={StaticResource colourConverter}}"
Text="{Binding ???, StringFormat='p2'}" />
</DataTemplate>
<UserControl.Resources>
What I don't understand is how to make the binding work for more than one column. What do I need to put in there?
Here's a post I put on the Silverlight forum (http://forums.silverlight.net/p/245344/615661.aspx/1?p=True&t=634611643101728290) which explains a bit more:
Hi,
I realise that this has been asked before in the forums, but I've yet to see a good answer, I guess it's either not possible or maybe so obvious I'm missing the answer!?
I'm using Silverlight 5 and I have a datagrid where each row represents a year and each column a month. For each month I am showing a total percentage value which is relevant to my application.
I have a collection of monthly total objects where the class looks something like this:
public class MonthlyTotals
{
public int Year { get; set; }
public decimal Jan { get; set; }
public decimal Feb { get; set; }
public decimal Mar { get; set; }
public decimal Apr { get; set; }
public decimal May { get; set; }
public decimal Jun { get; set; }
public decimal Jul { get; set; }
public decimal Aug { get; set; }
public decimal Sep { get; set; }
public decimal Oct { get; set; }
public decimal Nov { get; set; }
public decimal Dec { get; set; }
public decimal YearlyTotal { get; set; }
}
Each month column in the data grid is defined with a datatemplate like this:
<sdk:DataGridTemplateColumn Width="Auto"
CanUserSort="False"
Header="Jan">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Right"
VerticalAlignment="Center"
Foreground="{Binding Jan,
Converter={StaticResource colourConverter}}"
Text="{Binding Jan,
StringFormat='p2'}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn Width="Auto"
CanUserSort="False"
Header="Feb">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Right"
VerticalAlignment="Center"
Foreground="{Binding Feb,
Converter={StaticResource colourConverter}}"
Text="{Binding Feb,
StringFormat='p2'}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn Width="Auto"
CanUserSort="False"
Header="Mar">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Right"
VerticalAlignment="Center"
Foreground="{Binding Mar,
Converter={StaticResource colourConverter}}"
Text="{Binding Mar,
StringFormat='p2'}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
This is repeated for all 12 months plus the yearly total.
My question is, given that the datatemplate is the same for every column, how do I define it once and reuse it. I realise that I can define it in the static resources but I don't understand how to do the binding given that each column is bound to a different field?
I want to do something like this:
In the datagrid:
<sdk:DataGridTemplateColumn Width="Auto"
CanUserSort="False"
Header="Jan" CellTemplate={StaticResource columnTemplate} />
In the static resources I would have something like this:
<UserControl.Resources>
<DataTemplate x:Key="columnTemplate">
<TextBlock HorizontalAlignment="Right"
VerticalAlignment="Center"
Foreground="{Binding ???,
Converter={StaticResource colourConverter}}"
Text="{Binding ???, StringFormat='p2'}" />
</DataTemplate>
<UserControl.Resources>
What I don't understand is how to make the binding work for more than one column. What do I need to put in there?