I'm applying the following style to my DataGridColumns (via their HeaderStyle
property):
<Style x:Key="MyHeaderStyle" TargetType="DataGridColumnHeader">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<TextBlock Text="???" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The same style is applied to each column but I need some way of altering the content (such as the TextBlock text) depending on which column it is. I believe the DataGridColumnHeader has no binding of its own, so how could I achieve this? The number of columns will vary, so I can't simply create a separate style for each.
Use a TemplateBinding - it allows you to reach out of your ControlTemplate to get information about the actual control using this control template. My sample will bind the textblock to the content that's declared in the normal way for a DataGridColumHeader.
<Style x:Key="ColumnHeaderStyle" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DataGridColumnHeader">
<TextBlock Text="{TemplateBinding Content}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>