在我的窗口中,两个面板由网格拆分器隔开。分离器功能正常运行。当底部面板关闭时,我希望顶部面板占据总的屏幕空间(类似于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);
}
}
}
有人能建议任何方法或解决方案来满足我的要求吗,即在关闭底部面板时,顶部面板会占据所有可用空间?
谢谢
答案 0 :(得分:0)
您可以从以下高度开始:
<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);
}