如何在 WPF 中自动调整 GridViewColumn 数据大小并右对齐?
如何:
- 右对齐 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 工作得很好,自动宽度当然也能工作,但是当用长于列宽的数据更新 ObservativeCollection“集合”时,列大小不会自行更新所以这只是数据初始显示的解决方案:
<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>
How can I:
- right-align the text in the ID column
- make each of the columns auto size according to the text length of the cell with the longest visible data?
Here is the code:
<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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
要使每列自动调整大小,您可以在 GridViewColumn 上设置 Width="Auto"。
要右对齐 ID 列中的文本,您可以使用 TextBlock 创建单元格模板并设置 TextAlignment。 然后设置ListViewItem.HorizontalContentAlignment(在ListViewItem上使用带有setter的样式)以使单元格模板填充整个GridViewCell。
也许有一个更简单的解决方案,但这应该可行。
注意:该解决方案需要 Window.Resources 中的 HorizontalContentAlignment=Stretch 和 CellTemplate 中的 TextAlignment=Right。
To make each of the columns autosize you can set Width="Auto" on the GridViewColumn.
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.
Note: the solution requires both HorizontalContentAlignment=Stretch in Window.Resources and TextAlignment=Right in the CellTemplate.
如果内容的宽度发生变化,您将必须使用这段代码来更新每一列:
每次该列的数据更新时您都必须触发它。
If the width of the contents changes, you'll have to use this bit of code to update each column:
You'd have to fire it each time the data for that column updates.
如果您的列表视图也在调整大小,那么您可以使用行为模式来调整列的大小以适应整个 ListView 宽度。 与使用 grid.column 定义几乎相同
请参阅以下链接以获取一些示例并链接到源代码
http://lazycowprojects.tumblr.com/post/7063214400 /wpf-c-listview-column-width-auto
If your listview is also re-sizing then you can use a behavior pattern to re-size the columns to fit the full ListView width. Almost the same as you using grid.column definitions
See the following link for some examples and link to source code
http://lazycowprojects.tumblr.com/post/7063214400/wpf-c-listview-column-width-auto
我创建了以下类,并在应用程序中任何需要的地方使用它来代替 GridView:
I have created the following class and used across the application wherever required in place of
GridView
:由于我有一个 ItemContainerStyle 我必须将 HorizontalContentAlignment 放入 ItemContainerStyle
Since I had an ItemContainerStyle I had to put the HorizontalContentAlignment in the ItemContainerStyle
我喜欢 user1333423 的解决方案,除了它总是重新调整每一列的大小; 我需要允许某些列的宽度固定。 因此,在此版本中,宽度设置为“自动”的列将自动调整大小,而设置为固定量的列将不会自动调整大小。
I liked user1333423's solution except that it always re-sized every column; i needed to allow some columns to be fixed width. So in this version columns with a width set to "Auto" will be auto-sized and those set to a fixed amount will not be auto-sized.
我知道这已经太晚了,但这是我的方法:
主要思想是将 cellTemplete 元素的宽度绑定到 ViewGridColumn 的宽度。 Width=100 是第一次调整大小之前使用的默认宽度。 后面没有任何代码。 一切都在 xaml 中。
I know that this is too late but here is my approach:
The main idea is to bind the width of the cellTemplete element to the width of the ViewGridColumn. Width=100 is default width used until first resize. There isn't any code behind. Everything is in xaml.
我对接受的答案遇到了麻烦(因为我错过了 HorizontalAlignment=Stretch 部分并调整了原始答案)。
这是另一种技术。 它使用带有 SharedSizeGroup 的 Grid。
注意: ListView 上的Grid.IsSharedScope=true。
I had trouble with the accepted answer (because I missed the HorizontalAlignment=Stretch portion and have adjusted the original answer).
This is another technique. It uses a Grid with a SharedSizeGroup.
Note: the Grid.IsSharedScope=true on the ListView.
我创建了一个用于更新列表的 GridView 列标题的函数,并在调整窗口大小或列表视图更新其布局时调用它。
I created a function for updating GridView column headers for a list and call it whenever the window is re-sized or the listview updates it's layout.
要解决上述问题,可以执行以下操作。
附加即使您的列表视图也发生更改的集合,如下所示,
还声明一个私有 maxWidth 属性来存储您的视图遇到的最长内容。
现在处理程序如下所示,
此外,一旦启动对话框,窗口中可能已经填充了多行,并且顶行不是最长的。 仅当集合更改时才会触发上述代码,但由于数据已加载并且加载对话框时的可见行不是最宽的行,因此上述代码将无法调整列的大小。 为了解决此问题,请在 ListView_OnPreviewMouseLeftButton 事件上调用上述处理程序。 如下所示,
一旦有人滚动到视图的最宽行内容,上面的代码将刷新您的列宽。
To fix the above issue following can be done.
Attach the collection changed even of your list view like below,
also declare a private maxWidth property to store the longest content your view has encountered.
Now the handler is as below,
Also there can be chances that once you launch the dialog already the widow is populated with multiple rows and the top rows are not the longest. The above code will be triggered only when the collection changes but since the data is already loaded and the visible rows on loading the dialog were not the widest ones the above code will not be able to resize the column. In order to fix this issue call the above handler on the ListView_OnPreviewMouseLeftButton event. like below,
The above code will refresh your column width once someone scrolls to the widest row content of the view.
可以通过禁用虚拟化并在
GridViewColumn
上设置Width="Auto"
来将列自动调整为最长文本大小。默认情况下启用虚拟化,这意味着仅呈现可见行。 这对性能有好处。 缺点是仅使用当前可见的行来计算列宽 (
Width="Auto"
)。 禁用虚拟化意味着即使某行当前不可见,也会呈现所有行。 结果是自动宽度计算考虑所有行的值。Auto sizing a column to the longest text can be archived by disabling virtualisation and setting
Width="Auto"
on theGridViewColumn
.Virtualization is enabled by default and means that only visible rows are rendered. This is good for performance. The downside is that only the currently visible rows are used to calculate the column width (
Width="Auto"
). Disabling Virtualization means that all rows are rendered even if a row is not visible at the moment. The result is that Auto width calculation considers the values of all rows.这是你的代码
试试这个
This is your code
Try this
好吧,我刚刚遇到这个问题,我用 IValueConverter 解决了它。
GridViewColumns 数量
注意:8 是Xaml 中的
:不要忘记包含转换器文件。 在本例中,我在 App.xaml 中定义了转换器
Well, I just came to this problem and I solved it with a IValueConverter.
Notice: That 8 is the number of GridViewColumns
Now in the Xaml:
Don't forget to include the converter file. In this case, I have defined the converter in App.xaml