tags:

views:

28

answers:

1

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 ?

+1  A: 

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>
Quartermeister