Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF ListView Column Header alignment

Tags:

listview

wpf

xaml

I'm writing a WPF windows app, and the MainWindow contains a ListView control with 3 columns.

The following code displays the text in the column header centred (by default).

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListView>
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Column 1" 
                                    Width="200"/>
                    <GridViewColumn Header="Column 2" 
                                    Width="200"/>
                    <GridViewColumn Header="Column 3" 
                                    Width="200"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

And I found the following How to align the column header in a WPF ListView_GridView control article that shows how to left align the column header text. I've pasted the column below:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="{x:Type GridViewColumnHeader}">
            <Setter Property="HorizontalContentAlignment" Value="Left" />
        </Style>
    </Window.Resources>
    <Grid>
        <ListView>
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Column 1" 
                                    Width="200"/>
                    <GridViewColumn Header="Column 2" 
                                    Width="200"/>
                    <GridViewColumn Header="Column 3" 
                                    Width="200"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

But I need the first column header text to be left aligned, and the 2nd and 3rd column header text to be centred (like in the picture). enter image description here

Can someone please show me how to left align the column header text in the 1st column. And how to centre the column header text in the 2nd and 3rd columns?

like image 385
m_collard Avatar asked May 22 '17 17:05

m_collard


2 Answers

Set the HeaderContainerStyle property of the first column only and remove the implict Style from <Window.Resources>:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListView>
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Column 1" Width="200">
                        <GridViewColumn.HeaderContainerStyle>
                            <Style TargetType="{x:Type GridViewColumnHeader}">
                                <Setter Property="HorizontalContentAlignment" Value="Left" />
                            </Style>
                        </GridViewColumn.HeaderContainerStyle>
                    </GridViewColumn>
                    <GridViewColumn Header="Column 2" Width="200"/>
                    <GridViewColumn Header="Column 3" Width="200"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>
like image 71
mm8 Avatar answered Nov 05 '22 18:11

mm8


gg eazy

<ListView.Resources>
  <Style TargetType="{x:Type GridViewColumnHeader}">
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
  </Style>
</ListView.Resources>

Update: reviewing 1.5yr later, OP seemed to have found alignment for all columns as above; the actual question was to distinguish between columns for which the answer is actually provided by @mm8. Misread, oops.

like image 22
EricG Avatar answered Nov 05 '22 18:11

EricG