The easiest way to do this is to use a
ScrollViewer
around the StackPanel.
For example here is my XAML:
<window x:class="LayoutWithStackPanel.MainWindow" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<grid>
<grid.rowdefinitions>
<rowdefinition height="Auto" />
<rowdefinition height="*" />
</grid.rowdefinitions>
<stackpanel orientation="Horizontal">
<button content="Create" width="75" click="Button_Click" />
<button content="Up" width="75" click="ButtonUp_Click" />
<button content="Down" width="75" click="ButtonDown_Click" />
</stackpanel>
<scrollviewer grid.row="1" name="SvViewer" verticalscrollbarvisibility="Hidden">
<stackpanel name="StackPanel1" orientation="Vertical">
</stackpanel>
</scrollviewer>
</grid>
</window>
This will create a page with 3 buttons arranged along the top and a StackPanel below that will contain the generated buttons. Note that I've hidden the scrollbar for the ScrollViewer.
I placed your code into the Button_Click event to create lots of controls in the StackPanel StackPanel1.
The events for the other two buttons consist simply of
private void ButtonUp_Click(object sender, RoutedEventArgs e)
{
SvViewer.LineUp();
}
private void ButtonDown_Click(object sender, RoutedEventArgs e)
{
SvViewer.LineDown();
}
or
private void ButtonUp_Click(object sender, RoutedEventArgs e)
{
SvViewer.PageUp();
}
private void ButtonDown_Click(object sender, RoutedEventArgs e)
{
SvViewer.PageDown();
}
depending on how far you want to scroll.