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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With