Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 698
oscilatingcretin Avatar asked Jun 10 '11 13:06

oscilatingcretin


1 Answers

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.

like image 121
Martin Moser Avatar answered Oct 19 '22 13:10

Martin Moser