如何在 WPF 中自动调整和右对齐 GridViewColumn 数据? [英] How to autosize and right-align GridViewColumn data in WPF?
问题描述
我该怎么做:
- 右对齐 ID 列中的文本
- 根据具有最长可见数据的单元格的文本长度使每一列自动调整大小?
代码如下:
<ListView Name="lstCustomers" ItemsSource="{Binding Path=Collection}">
<ListView.View>
<GridView>
<GridViewColumn Header="ID" DisplayMemberBinding="{Binding Id}" Width="40"/>
<GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}" Width="100" />
<GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}"/>
</GridView>
</ListView.View>
</ListView>
部分答案:
感谢 Kjetil,GridViewColumn.CellTemplate 运行良好,自动宽度当然可以运行,但是当 ObservativeCollectionCollection"更新为长于列宽的数据时,列大小不会自行更新,因此这只是一个数据初始显示的解决方法:
partial answer:
Thanks Kjetil, the GridViewColumn.CellTemplate works well and the Auto Width works of course but when the ObservativeCollection "Collection" is updated with longer-than-column-width data, the column sizes do not update themselves so that is only a solution for the initial display of data:
<ListView Name="lstCustomers" ItemsSource="{Binding Path=Collection}">
<ListView.View>
<GridView>
<GridViewColumn Header="ID" Width="Auto">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Id}" TextAlignment="Right" Width="40"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}" Width="Auto" />
<GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}" Width="Auto"/>
</GridView>
</ListView.View>
</ListView>
推荐答案
要使每一列自动调整大小,您可以在 GridViewColumn 上设置 Width="Auto".
To make each of the columns autosize you can set Width="Auto" on the GridViewColumn.
要右对齐 ID 列中的文本,您可以使用 TextBlock 创建单元格模板并设置 TextAlignment.然后设置ListViewItem.HorizontalContentAlignment(在ListViewItem上使用带有setter的样式)使单元格模板填满整个GridViewCell.
To right-align the text in the ID column you can create a cell template using a TextBlock and set the TextAlignment. Then set the ListViewItem.HorizontalContentAlignment (using a style with a setter on the ListViewItem) to make the cell template fill the entire GridViewCell.
也许有更简单的解决方案,但这应该可行.
Maybe there is a simpler solution, but this should work.
注意:该解决方案需要 Window.Resources 中的 HorizontalContentAlignment=Stretch 和 CellTemplate 中的 TextAlignment=Right.
Note: the solution requires both HorizontalContentAlignment=Stretch in Window.Resources and TextAlignment=Right in the CellTemplate.
<Window x:Class="WpfApplication6.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</Window.Resources>
<Grid>
<ListView Name="lstCustomers" ItemsSource="{Binding Path=Collection}">
<ListView.View>
<GridView>
<GridViewColumn Header="ID" Width="40">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Id}" TextAlignment="Right" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}" Width="Auto" />
<GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}" Width="Auto"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
这篇关于如何在 WPF 中自动调整和右对齐 GridViewColumn 数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!