0

I'm trying to display image results on a StackPanel, and scroll through the results. However the height of the ScrollViewer is set to the height of the StackPanel instead of the available space in the Window.

<Grid x:Name="maingrid" Background="#222">
        <StackPanel>
            <Grid>
                <!--Search bar and stuff-->
            </Grid>
            
            <Grid>
                <ScrollViewer>
                    <StackPanel x:Name="resultsparent" VerticalAlignment="Top" HorizontalAlignment="Stretch" Width="1025">

                    </StackPanel>
                </ScrollViewer>
            </Grid>
        </StackPanel>
    </Grid>

I tried to set the height of the ScrollViewer manually and it works, but I don't know how to make it automatically match the borders and I'd like some help on this please.

EDIT: It works with a DockPanel instead of the StackPanel, but it's horizontal and I can't see the Orientation property.

Hokome
  • 3
  • 2

1 Answers1

0

The problem is the outer StackPanel. A StackPanel is allowed to grow to fit the content, so it is stretching beyond the boundaries of your window. Try a different panel instead, such as a DockPanel.

For example:

<Grid x:Name="maingrid" Background="#222">
    <DockPanel>
        <Grid>
            <!--Search bar and stuff-->
        </Grid>

        <Grid>
            <ScrollViewer>
                <StackPanel x:Name="resultsparent" VerticalAlignment="Top" HorizontalAlignment="Stretch">

                </StackPanel>
            </ScrollViewer>
        </Grid>
    </DockPanel>
</Grid>
Jason Tyler
  • 1,331
  • 13
  • 26
  • It works but now it's horizontal, and I don't see the Orientation property. – Hokome Jun 29 '20 at 13:51
  • I'm not sure what you mean by "now it's horizontal". After that change, you should have a vertical scrollbar. Is that not what want? – Jason Tyler Jun 29 '20 at 14:04
  • No it was stacking horizontally, but I used DockPanel.Dock and now it works fine. Thank you! – Hokome Jun 29 '20 at 14:08