WPF GridViewColumn Width=“auto” only works for items in the current scroll scope

倾然丶 夕夏残阳落幕 提交于 2019-12-04 01:26:09

问题


I have a ListView that contains many items and am trying to set the column widths to auto so that they auto-expand to the width of the longest string in the column. At first, it appeared to work, but as I scrolled down the list, I noticed that some of the longer strings were cut short because the column didn't auto-expand enough. Then it occurred to me that setting the width to auto seems to calculate the width based on the column values visible on the grid at the time. So, when I scroll down to rows containing longer strings for a particular column, I can double-click the column divider to have it expand further. This behavior doesn't seem right.

How can I get the column to expand to the length of the longest string from the start?


回答1:


The reason is that virtualization prevents some items from being generated, and then they are not considered for the calculation of the width.

So you can switch off the virtualization for the ListView with adding this to it ->

<ListView x:Name="lv" ScrollViewer.CanContentScroll="False">

But be careful it can slow down your app, if you have a lot of items, as they all will be generated at startup.



来源:https://stackoverflow.com/questions/6307289/wpf-gridviewcolumn-width-auto-only-works-for-items-in-the-current-scroll-scope

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!