Hexum064
Hexum064

Reputation: 349

HeaderTemplate on DataGrid

I have created a DataTemplate to use for the HeaderTemplate of a DataGrid. I would like to have the template grab the Header properties text, which would be set to the name of the column so that it can use it for the text of a control in the DataTemplate.

Here is the DataTemplate

<DataTemplate x:Key="AncillaryHeaderTemplate">
    <Border Background="Transparent" BorderThickness="0" Height="60" Name="ab">
        <TextBlock TextAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap"
                   Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                        AncestorType={x:Type DataGridTextColumn}}, Path=Header, Mode=TwoWay}" />
    </Border>
</DataTemplate>

And here is the DataGrid

    <DataGrid Name="AncillaryGrid">

        <DataGrid.Columns>
            <DataGridTextColumn Header="Account" HeaderStyle="{StaticResource AncillaryHeaderStyle}" HeaderTemplate="{StaticResource AncillaryHeaderTemplate}" />

Is there a better way to have a DataTemplate and pass the title of the column to it or am I approaching this wrong?

Upvotes: 0

Views: 1074

Answers (1)

user128300
user128300

Reputation:

You simply can do the following, since the data context of the header template is the content of the header (i.e. the value of the DataGridTextColumn.Headerproperty):

<TextBlock TextAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap"
    Text="{Binding }" />

Upvotes: 3

Related Questions