'The ScrollViewer automatically changes its height and does not scroll

In my program, I use ScrollViewer to display elements inside, but the problem is that when the ScrollViewer is filled with content, its height automatically changes and it becomes impossible to scroll since the height is equivalent to the content. I put a ScrollViewer inside a Grid and it automatically stretches to its full height VerticalAlignment="Stretch". I can't pre-limit its height because its height automatically adjusts to its parent to fill all the space. How can I solve this?

<Grid Grid.Column="0" RowSpacing="10">
        <Grid.RowDefinitions>
            <RowDefinition MaxHeight="35"/>
            <RowDefinition MaxHeight="35"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        
        <Grid Grid.Row="2">
            <ScrollViewer VerticalAlignment="Stretch" VerticalScrollMode="Enabled" VerticalScrollBarVisibility="Visible" HorizontalScrollMode="Disabled">
                <ItemsControl x:Name="notesContent" Loaded="NotesContent_Loaded" Margin="0,0,15,0">
                    <ItemsControl.ItemContainerTransitions>
                        <TransitionCollection>
                            <AddDeleteThemeTransition>
                            </AddDeleteThemeTransition>
                        </TransitionCollection>
                    </ItemsControl.ItemContainerTransitions>
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                </ItemsControl>
            </ScrollViewer>
        </Grid>
    </Grid>


Solution 1:[1]

The ScrollViewer automatically changes its height and does not scroll

The ScrollViewer parent is Grid, and you have set it's VerticalAlignment proeprty as Stretch, it will vertically fill into grid. And it is by design, you can refer to Grid document.

And if you want to make ScrollViewer scrollable, you need to make the content large than ScrollViewer actual height, and the better way is specific height value for ScrollViewer.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nico Zhu - MSFT