简体   繁体   中英

WPF ListView Column Header alignment

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). 在此处输入图片说明

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?

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM