网格拆分器行为(底部面板关闭时展开顶部面板)

Grid splitter behavior (Expand top panel when bottom panel closes)

在我的 window 中,两个面板由网格分隔器分隔。拆分器功能正常工作。当底部面板关闭时,我希望顶部面板占据整个屏幕 space(类似于 visual studio IDE),但是当我关闭面板时,它会留下空白 space。下面给出了演示此问题的代码:

XAML

<Window x:Class="WpfApp1.Window1"
        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"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="Window1" Height="450" Width="800" WindowState="Maximized">
    <Grid x:Name="grid">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <StackPanel x:Name="panel1" Grid.Row="0" Background="Bisque" Margin="3" Orientation="Vertical">
            <Button Height="50" Content="Button 1" Margin="5"/>
            <Button Height="50" Content="Button 2" Margin="5"/>
            <Button Height="50" Content="Button 3" Margin="5"/>
        </StackPanel>
        <GridSplitter Grid.Row="1" Height="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ShowsPreview="True" ResizeDirection="Rows"/>
        <StackPanel x:Name="panel2" Grid.Row="2" Background="AliceBlue" Margin="3" Orientation="Vertical">
            <Button Content="X" HorizontalAlignment="Right" VerticalAlignment="Top" Click="Button_Click"/>
            <Button Height="50" Content="Button 4" Margin="5"/>
            <Button Height="50" Content="Button 5" Margin="5"/>
            <Button Height="50" Content="Button 6" Margin="5"/>
        </StackPanel>
    </Grid>
</Window>

代码隐藏

using System.Windows;

namespace WpfApp1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            grid.Children.Remove(panel2);
        }
    }
}

任何人都可以提出任何方法或解决方案来实现我的要求,即在关闭底部面板时,顶部面板占据所有可用 space?

谢谢

您可以从这些高度开始:

<Grid.RowDefinitions>
    <RowDefinition Height="*"/>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="*" />
</Grid.RowDefinitions>

...并在删除 StackPanel:

时将最后一个的 Height 设置为 Auto
private void Button_Click(object sender, RoutedEventArgs e)
{
    grid.Children.Remove(panel2);
    grid.RowDefinitions[Grid.GetRow(panel2)].Height = new GridLength(1, GridUnitType.Auto);
}