gpt4 book ai didi

WPF ListView 列标题对齐

转载 作者:行者123 更新时间:2023-12-01 08:32:11 26 4
gpt4 key购买 nike

我正在编写一个 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>

但我需要第一列标题文本左对齐,第二和第三列标题文本居中(如图所示)。
enter image description here

有人可以告诉我如何左对齐第一列中的列标题文本。以及如何将列标题文本居中放置在第 2 列和第 3 列中?

最佳答案

设置 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>

关于WPF ListView 列标题对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44119146/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com