在GridSplitter下折叠控制 – 出现不需要的空白区域

鉴于以下XAML;

            

Button_Click位置;

 this.LowerPanel.Visibility = this.LowerPanel.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible; 

这看起来像这样;

在此处输入图像描述

如果在执行任何其他操作之前单击该按钮,则它会按预期折叠;

在此处输入图像描述

但是,如果我使用网格分割器resize..

在此处输入图像描述

然后当我按下按钮时,会发生以下情况;

在此处输入图像描述

有没有办法让下面的元素在resize后再次正确折叠?

注意:出于这个问题的目的,我刚刚使用了代码隐藏事件处理程序和一个按钮来触发它,但在现实生活中它以适当的MVVM方式完成,可见性由视图模型上的绑定属性决定。

AFAIK,当使用GridSplitter时,它会重写相应RowDefinitions和ColumnDefinitions的Height或Width属性。 为了做你想做的事,你应该使用RowDefinitions.Height属性,如下所示:

 public MainWindow() { InitializeComponent(); //gr is the name of the Grid basepr1 = gr.RowDefinitions[0].Height; basepr2 = gr.RowDefinitions[2].Height; } static GridLength zero = new GridLength(0); GridLength basepr1; GridLength basepr2; private void Button_Click(object sender, RoutedEventArgs e) { if (this.LowerPanel.Visibility == Visibility.Visible) { this.LowerPanel.Visibility = Visibility.Collapsed; basepr2 = gr.RowDefinitions[2].Height; // remember previos height gr.RowDefinitions[2].Height = zero; } else { this.LowerPanel.Visibility = Visibility.Visible; gr.RowDefinitions[2].Height = basepr2; } } 

我认为你需要将其中一个行定义标记为’*’: