Ok this is really making me crazy and I need some help.
I have a listview with an itemssource that has several gridviewcolumns. When the ItemsSource gets set, the columns size to fit the contents.
Whenever I increase the width by grabbing the column it makes the listview grow, but when I decrease the width it does not shrink. According to this post this is per WPF layout process.
I subclassed the ListView to override this behavior and can’t get it to work right.
I have overridden the MeasureOverride function like so:
protected override Size MeasureOverride(Size constraint)
{
var size = base.MeasureOverride(constraint);
var gv = this.View as GridView;
if (gv != null)
{
var width = gv.Columns.Select(a => a.ActualWidth).ToArray();
size.Width = gv.Columns.Sum(a => a.ActualWidth) ;
if (scroll.ScrollableHeight > 0)
size.Width += SystemParameters.ScrollWidth;
}
return size;
}
Which does cause the listview to shrink as desired, but if the listview has a vertical scrollbar it disappears.
I can’t figure out how to get the scrollbar to come back programmatically.
Now if I then increase the width beyond the original width the scrollbar comes back(but this is through the UI not code. I just have been playing with this for longer than I care to admit and I’m lost.
Anyone have any idea what I should do besides quit trying?
The reason this is even a problem is because of the very nature of a
ScrollViewer, it keeps the size of it’s content, so when theListViewshrinks via aMeasureOverride, it’s content still remains the same. The solution below is not perfect, but it is the best I could find. If there is a better one please let me know.The solution I arrived at was to keep the
MeasureOverridefunction as above and also overridingArrangeOverrideand set the width of theScrollViewerto be the final width of theListViewlike so: