Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: Setting the text alignment of a GridViewColumn

Tags:

listview

wpf

xaml

By default, text is centered in the headers of a ListView (not in the content cells!), I've been struggling to make it left aligned, here's what I've come up with:

<ListView.Resources>
    <DataTemplate x:Key="Templ">
       <TextBlock HorizontalAlignment="Left" Text="{Binding}"/>
    </DataTemplate>
</ListView.Resources>   
...
<GridViewColumn HeaderTemplate="{StaticResource Templ}">File</GridViewColumn>

This does seem to be the right place to alter the appearance of the header, since I can change other properties like Margin, etc., yet it does not respond to the HorizontalAlignment property! I'm guessing the textbox is sized to content and itself centered, thus making the alignment property redundant.

How can I make the text left aligned ?

like image 995
Saulius Valatka Avatar asked Jul 09 '10 15:07

Saulius Valatka


1 Answers

Set the HeaderContainerStyle to a style that sets HorizontalContentAlignment to left:

<ListView.Resources>
    <DataTemplate x:Key="Templ">
        <TextBlock HorizontalAlignment="Left" Text="{Binding}"/>
    </DataTemplate>
    <Style x:Key="HeaderStyle" TargetType="GridViewColumnHeader">
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
    </Style>
</ListView.Resources>
<ListView.View>
    <GridView>
        <GridView.Columns>
            <GridViewColumn HeaderTemplate="{StaticResource Templ}" HeaderContainerStyle="{StaticResource HeaderStyle}">File</GridViewColumn>
        </GridView.Columns>
    </GridView>
</ListView.View>
like image 163
Quartermeister Avatar answered Nov 09 '22 10:11

Quartermeister