Question

I have just started to experiment with gridsplitters and have stumbled across a very weird behaviour and I cannot seem to fix it. Apparently the others have similar issues (according to google), but there were no helpful comments.

I have a grid with 2 rows. On start up the bottom one has a Hight of 250. The top row takes the rest with *. When I resize the rows with the splitter the behaviour is as expected for the top row. But when I drag the splitter upwards and past the program window, the content of the bottom row will drop out of the window (=move downwards until it is gone). I'd expect that I cannot make each row larger than the parent container.

<Grid x:Name="grid_main" ScrollViewer.VerticalScrollBarVisibility="Disabled" >
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition MinHeight="250"  Height="250"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <GridSplitter x:Name="splitter" 
        ResizeDirection="Rows" Grid.Column="0" Grid.ColumnSpan="1" Grid.Row="0" Width="Auto"
        Height="5" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Margin="0" ResizeBehavior="BasedOnAlignment" />
    <Grid Grid.Column="0" Grid.Row="0">
    </Grid>
    <Grid Grid.Column="0" Grid.Row="1">              
    </Grid>
</Grid>

This is my code. The content of both rows is again hosted in grids and has been removed for the sake of simplicity. Again: Resizing for the top row works fine. But the bottom row can be resized to infinity. It works as expected if I hard-code a MaxHeight. But that has to depend on the window size.

Was it helpful?

Solution

Try changing your second RowDefinition to the following:

<RowDefinition MinHeight="250" MaxHeight="{Binding ElementName=grid_main, Path=ActualHeight}" Height="250"/>

This will ensure that the row height will not exceed the window size.

OTHER TIPS

Richard's solution didn't work for me (the ActualHeight of the grid expanded past the window size along with the row height).

Using Snoop, I found that the ActualHeight of an ancestor ContentPresenter wasn't increasing. Thus the following bottom row definition worked for me, although I was still seeing issues if I set MinHeight on either the top or bottom rows:

<RowDefinition Height="430"
               MaxHeight="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}}, Path=ActualHeight}"/>

This problem only occurs because the second row has an absolute Height. If you actually want an absolute Height value try Richards's solution. Otherwise, just use a relative Height (e.g. 2*) on the second row as well.

Grigory Zhadko's answer is good to me.

I had a problem with this code

gridBase.RowDefinitions[1].Height = (GridLength)gridLengthConverter.ConvertFrom("1");
gridBase.RowDefinitions[3].Height = (GridLength)gridLengthConverter.ConvertFrom("1");

and I fixed it like this

gridBase.RowDefinitions[1].Height = new GridLength(0.1, GridUnitType.Star);
gridBase.RowDefinitions[3].Height = new GridLength(0.1, GridUnitType.Star);

and the grid converter works perfectly

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