Making a StackPanel scrollable in Windows Phone 8

2019-05-20 21:31发布

问题:

I have a list of controls, put into a stackpanel. These controls are Hubtiles, that are added programatically after a user creates it - a list essentially. I need to make the StackPanel in which they are added, scrollable - what would be the best approach for this? Should i put the stackpanel inside a ScrollView, and then increase the StackPanels height with the actual height of the Hubtile - to make it scrollable, but not so that the user can scroll infinite without something being there. So whenever the amount of controls inside the StackPanel reaches 3, it'll automatically increase it's height like this:

Whenever the amount of controls inside the StackPanel reaches 3 or above:

StackPanel.Height = StackPanel.Height + Hubtile.ActualHeight;

Thanks a lot!

回答1:

Just put the StackPanel into ScrollViewer, set the StackPanel.VerticalAlignment to Stretch and set fixed size to the parent ScrollViewer. This is necessary - the ScrollViewer must know its size, to show the scrollbars for the inner content when the inner content is too long.