Reputation: 11
I have a WPF control with this basic structure:
<Grid>
<ScrollViewer x:Name="_Scroll" Grid.Row="4" Grid.RowSpan="20" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" CanContentScroll="True" >
<StackPanel x:Name="_Panel" />
</ScrollViewer>
</Grid>
Some Grid
items are added to _Panel later, during execution, thus generating a list of items vertically aligned. It all works properly when the scrollbar isn't visible, but when I actually need to scroll the content, the StackPanel overflows and I end up seeing part of the StackPanel's content above the ScrollViewer. Any idea as to what could be causing this issue? If you need more details, let me know and I'll edit the question appropriately.
EDIT: Added an image explaining the issue better: above is what the control looks like before beginning to scroll, below is the issue with the items overflowing over the ScrollViewer. As for the code, there's nothing particular about the insertion of items in the panel, it's basically among the lines of:
Dim nRow As New Grid
' Insertion of subcontrols within nRow
_Panel.Children.Add(nRow)
Upvotes: 0
Views: 1622
Reputation: 726
this is because the amount of items added later in your stackpanel does not update the height of the scroll bar, which is external to it, and refers only to the original size of the stackpanel you can solve as folowing:
[Edit] give your stackpanel a relative or absolute size for the scroll to be displayed.
something like that:
<Grid Name="GridContent" Height="XXX" >
<StackPanel Height="{Binding ActualHeight, ElementName=GridContent}"
Orientation="Vertical"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.CanContentScroll="True">
</StackPanel>
</Grid>
Upvotes: 1