Reputation: 748
Very simple scenario But I have wasted lots of time, so I am asking here. I have a wpf application.
A listview is there with vertical scrollbar visibility to Auto and horizontal scrollbar visibility disabled. Now when I start adding item in it, like there is 2 items and no vertical scrollbar is there because of Auto. Now as I add 3rd item, a vertical scrollbar gets visible and content shifted towards left with some 15 pixel.
It looks weird and I want that content remains in the same position and only scrollbar get visible or collapsed.
I have tried setting margin to inner item or padding to listview. But nothing works.
Upvotes: 0
Views: 1225
Reputation: 935
Try the following: Add a scrollviewer control to your GUI, right click -> edit a template -> edit a copy
Now here´s the thing your looking for:
<ControlTemplate x:Key="ScrollViewerControlTemplateOwn" TargetType="{x:Type ScrollViewer}">
<Grid x:Name="Grid" Background="{TemplateBinding Background}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
The auto setting for the column and row definitions are causing the change in the width when scrollbars are visible or not. If you change those auto values to e.g. 20 the display won´t change in width or height. For adding such a scrollviewer to a listview, do the same "edit a copy" thing with the listview and set the scrollviewer to your "own" template like this:
<ControlTemplate TargetType="{x:Type ListView}">
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="1" SnapsToDevicePixels="true">
<ScrollViewer Focusable="false" Style="{Binding ScrollViewerControlTemplateOwn}" Padding="{TemplateBinding Padding}">
Hope that helps :)
Upvotes: 2