Grid grows when content grows, but does not shrink when content shrinks.
Posted: 02 Jun 2021, 14:38
I have a ScrollViewer ControlTemplate that looks like this (simplified for readability):
The ScrollViewer is used inside a ListView/GridView, and PART_ScrollContentPresenter displays the columns. If PART_ScrollContentPresenter grows (e.g. show hidden column), the Grid grows with it, so everything is displayed correctly. However if PART_ScrollContentPresenter shrinks, the Grid stays the same size, leaving a gap between PART_ScrollContentPresenter and PART_VerticalScrollbar.
How can I get the Grid to properly follow the size of its contents?
Code: Select all
<ControlTemplate TargetType="{x:Type ScrollViewer}">
<Grid>
<DockPanel>
<ScrollViewer>
<GridViewHeaderRowPresenter />
</ScrollViewer>
<ScrollContentPresenter
Name="PART_ScrollContentPresenter"
/>
</DockPanel>
<ScrollBar
Name="PART_VerticalScrollBar"
HorizontalAlignment="Right"
/>
</Grid>
</ControlTemplate>
How can I get the Grid to properly follow the size of its contents?