WPF ListView 列标题对齐
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44119146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF ListView Column Header alignment
提问by m_collard
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 controlarticle that shows how to left align the column header text. I've pasted the column below:
我在 WPF ListView_GridView 控件文章中找到了以下如何对齐列标题,该文章显示了如何左对齐列标题文本。我已经粘贴了下面的列:
<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 列中?
回答by mm8
Set the HeaderContainerStyleproperty 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>
回答by EricG
gg eazy
GG EAZY
<ListView.Resources>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Left"/>
</Style>
</ListView.Resources>

