Expander combined with GridSplitter

2019-04-11 01:32发布

问题:

I’m trying to split my WPF window into two “areas”, top and bottom.

  • The top area contains a grid.
  • The bottom area contains an expander.

Between the two areas should be a GridSplitter which the user can use to resize the areas.
The content of each area should use the full high of the area.

By default, the expander is expanded.
When the user closes the expander, the bottom area should reduce its height to the height of the collapsed expander.

This is my code:

<Window
    x:Class="App.Shell"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Title="Shell" Height="800" Width="1200">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid Name="MainContentGrid">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <!-- Top area -->
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"></RowDefinition>
                    <RowDefinition Height="*"></RowDefinition>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Button Grid.Row="0" Grid.Column="0">1</Button>
                <Button Grid.Row="1" Grid.Column="0">2</Button>
                <Button Grid.Row="0" Grid.Column="1">3</Button>
                <Button Grid.Row="1" Grid.Column="1">4</Button>
            </Grid>
            <GridSplitter Grid.Row="1" 
              HorizontalAlignment="Stretch" 
              VerticalAlignment="Top"
              Background="Black" 
              ShowsPreview="true"
              ResizeDirection="Rows"
              Height="5"></GridSplitter>
            <!-- Bottom area -->
            <Expander Grid.Row="1" Margin="0,5,0,0" IsExpanded="True" Height="Auto" VerticalAlignment="Stretch">
                <Border Background="Red" Height="Auto" MinHeight="100" VerticalAlignment="Stretch"></Border>
            </Expander>
        </Grid>
        <!-- Application Status Region -->
        <ContentControl prism:RegionManager.RegionName="{x:Static local:RegionNames.StatusRegion}" Grid.Row="1" />
    </Grid>
</Window>

Two things are not working:

  • The expander does not all available space (does not change its height)

  • When I close the expander, the GridSplitter does not allow the top area to use all the space available.

How can I make this work?

回答1:

Once you interact with GridSplitters they set concrete relative or absolute Height/Width values on the grid row/column definitions. So once you collapse the Expander you should set its row's Height to GridLength.Auto.