Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Text columns are defined with 
 in header text to multiline them:
C#
<DataGrid.Columns>
    ...
    <DataGridTextColumn Header="first&#x0a;secondline" />
    ...
</DataGrid.Columns>


How do I center each line of multiline column header?

What I have tried:

If I use DataGridColumnHeader style like this:
<Style TargetType="{x:Type DataGridColumnHeader}" x:Key="DataGridColumnHeaderStyle">
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
    </Style>

It will center content but not each line.
[    first         ]
[    secondline    ] 

I need this:
[      first       ]
[    secondline    ] 
Posted
Updated 21-Apr-23 2:34am
v2

I did a quick Google Search: wpf datagrid center multiline cell text - Google Search[^] and the first result returned this: Datagrid column formatting: how to specify multiline and right-align text?[^]. So, for you, rather than use "Right" alignment, change it to "Center" and it works.
 
Share this answer
 
final result
<Style TargetType="{x:Type DataGridColumnHeader}" x:Key="DataGridColumnHeaderStyle">
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock TextAlignment="Center"
                               VerticalAlignment="Center"
                               Text="{TemplateBinding Content}" />
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900