Поведение разделителя сетки (Разверните верхнюю панель, когда нижняя панель закроется) - PullRequest
0 голосов
/ 18 октября 2018

В моем окне две панели разделены сеткой.Функциональность Splitter работает правильно.Когда нижняя панель закрывается, я хочу, чтобы верхняя панель занимала все пространство экрана (аналогично Visual Studio IDE), однако когда я закрываю панель, она оставляет пустое пространство.Код, который демонстрирует эту проблему, приведен ниже:

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);
        }
    }
}

Может кто-нибудьпредложить какой-либо подход или решение для достижения моих требований, т.е. после закрытия нижней панели верхняя панель занимает все доступное пространство?

Спасибо

1 Ответ

0 голосов
/ 18 октября 2018

Вы можете начать с этих высот:

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

... и просто установить Height последней на Auto при удалении StackPanel:

private void Button_Click(object sender, RoutedEventArgs e)
{
    grid.Children.Remove(panel2);
    grid.RowDefinitions[Grid.GetRow(panel2)].Height = new GridLength(1, GridUnitType.Auto);
}
...