0
votes

I have a control in a grid row with height set to auto. the control's max height is bound to the actual height of the grid. This is so that the control will resize depending on it's content, but then not grow outside of the size of the grid if there is lots of data - it just gets a scroll bar. This works perfectly fine.

The issue is when I resize the window. If i make the window smaller than the size of the control, then the control doesn't resize to fit in the window.

If I then (with the smaller window size) close the screen and open it back up, the control resizes to the correct size.

When going from a smaller size to a larger size, the control expands to take up more space. It just doesn't resize to take up less space when going from a larger size to a smaller size

<Grid x:Name="MyGrid">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <ScrollViewer MaxHeight="{Binding ElementName=MyGrid, Path=ActualHeight}">
        <TextBlock Background="Gray" Text="Hello World" Height="700"/>
    </ScrollViewer>
</Grid>

When shrinking the window, I expect the control to shrink, but it stays the same height and stays partially off screen

2

2 Answers

1
votes

Remove MaxHeight and change Height of RowDefinition to "*"

<Grid x:Name="MyGrid">
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <ScrollViewer>
        <TextBlock
            Height="700"
            Background="Gray"
            Text="Hello World" />
    </ScrollViewer>
</Grid>
0
votes

It depends on exactly what you intend doing here.

You could simplify your markup from:

<Grid x:Name="MyGrid">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <ScrollViewer MaxHeight="{Binding ElementName=MyGrid, Path=ActualHeight}">
        <TextBlock Background="Gray" Text="Hello World" Height="700"/>
    </ScrollViewer>
</Grid>

To:

    <ScrollViewer>
        <TextBlock Background="Gray" Text="Hello World" Height="700"/>
    </ScrollViewer>

You only have one rowdefinition in the grid so there is no point in having the rowdefinition.

The grid only contains the scrollviewer - no point in the grid.

In fact, a textblock is designed to allow you to scroll long content and the template has it's own scrollviewer so you could just have a textblock:

<Window .....
    <TextBlock Background="Gray" 
               Text="Hello World" 
               Height="700"
               ScrollViewer.CanContentScroll="True"
               ScrollViewer.VerticalScrollBarVisibility="Auto"
               />
</Window>