简体   繁体   English

WPF ListView 列标题对齐

[英]WPF ListView Column Header alignment

I'm writing a WPF windows app, and the MainWindow contains a ListView control with 3 columns.我正在编写一个 WPF Windows 应用程序,MainWindow 包含一个具有 3 列的 ListView 控件。

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.在 WPF ListView_GridView 控件文章中找到了以下如何对齐列标题,该文章显示了如何左对齐列标题文本。 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?以及如何将列标题文本居中放置在第 2 列和第 3 列中?

Set the HeaderContainerStyle property of the first column only and remove the implict Style from <Window.Resources> :仅设置第一列的HeaderContainerStyle属性并从<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 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;更新:回顾 1.5 年之后,OP 似乎已经找到了所有列的对齐方式,如上; the actual question was to distinguish between columns for which the answer is actually provided by @mm8.实际问题是区分@mm8 实际提供答案的列。 Misread, oops.看错了,哎。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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