Question

I am new for WPF so apologies if the answer is so obvious. I got a WPF resizable window with a single stack panel control that is stretched vertically and horizontally to fill the window. On window activated event, I use ".Children.Add" to add button controls to the panel. I have no idea how many buttons will be there at runtime so I checked "CanVerticallyScroll" in the panel. ScrollViewer.VerticalScrollBarVisibility is set to Visible by default.

I am still not seeing scroll bars at runtime though.

What properties did I miss to show scrolling panel with buttons?

Thanks

XAML:

<Window x:Class="ResMed.Ecp.Utility.ConnectionWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ConnectionWindow" Height="388" Width="641.6" Activated="Window_Activated">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="359*"/>
        </Grid.RowDefinitions>
        <StackPanel x:Name="pnlConnectionButtons" Margin="10,10.2,10.2,10" Grid.Row="1" CanVerticallyScroll="True"/>

    </Grid>
</Window>

Code behind:

private void Window_Activated(object sender, EventArgs e)
{
    for (int i = 0; i < 20; i++)
    {
        Button btn = new Button();
        btn.Content = "Hello";
        pnlConnectionButtons.Children.Add(btn);
    }
}
Was it helpful?

Solution

Place your StackPanel inside a ScrollViewer:

<ScrollViewer>
    <StackPanel>
        <Button Content="Hello World"></Button>
        ...
        ...
    </StackPanel>
</ScrollViewer>

You can also remove CanVerticallyScroll="True". From MSDN:

This property is not intended for use in your code. It is exposed publicly to fulfill an interface contract (IScrollInfo). Setting this property has no effect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top