Expander combined with GridSplitter

隐身守侯 提交于 2019-12-06 07:28:23

问题


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.



来源:https://stackoverflow.com/questions/38393033/expander-combined-with-gridsplitter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!