我正在编写一个WPF Windows应用程序,而MainWindow包含一个包含3列的ListView控件。
以下代码显示列标题中的文本(默认情况下)。
<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>
我找到了以下How to align the column header in a WPF ListView_GridView control文章,其中展示了如何左对齐列标题文本。我粘贴了下面的专栏:
<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>
但我需要将第一列标题文本左对齐,并将第2和第3列标题文本放在中心(如图所示)。
有人可以告诉我如何左对齐第1列中的列标题文本。以及如何将列标题文本置于第2和第3列的中心?
答案 0 :(得分:2)
仅设置第一列的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>
答案 1 :(得分:1)
gg eazy
<ListView.Resources>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Left"/>
</Style>
</ListView.Resources>