【问题标题】:Collapse Control under a GridSplitter - Unwanted White space appearsGridSplitter 下的折叠控件 - 出现不需要的空白
【发布时间】:2014-07-31 20:57:18
【问题描述】:

给定以下 XAML;

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition MinHeight="100"/>
        <RowDefinition Height="2"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <Border Grid.Row="0"
            Background="LightBlue">
        <Button Height="30"
                Click="Button_Click">Hide Lower Panel</Button>
    </Border>
    <GridSplitter Grid.Row="1"
                  ResizeDirection="Rows" 
                  Width="Auto"
                  HorizontalAlignment="Stretch"
                  Margin="0"
                  x:Name="Splitter"/>
    <Border Grid.Row="2"
            Background="LightCoral"
            x:Name="LowerPanel" MinHeight="25"/>
</Grid>

Button_Click 在哪里;

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

看起来像这样;

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

但是,如果我使用网格拆分器调整大小..

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

有没有办法让下部元素在调整大小后再次正确折叠?

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

【问题讨论】:

  • 如果再次将边框的可见性设置为可见,边框的期望高度是多少?
  • 两种结果都可以——要么恢复默认值,要么恢复到折叠时的大小。

标签: c# wpf


【解决方案1】:

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

【讨论】:

  • 感谢 stukselbax,这提供了我正在寻找的行为。我必须想办法把它与我的视图模型和数据绑定设置联系起来,这才是真正有趣的开始!再次欢呼。
【解决方案2】:

我认为您需要将其中一个行定义标记为“*”:

<Grid.RowDefinitions>
    <RowDefinition MinHeight="100"/>
    <RowDefinition Height="2"/>
    <RowDefinition Height="*"/>
</Grid.RowDefinitions>

【讨论】:

  • 嗯,不,这似乎没有做到。它实际上在它确实工作的一个实例中停止了它的工作(在手动调整大小之前)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-06
  • 1970-01-01
  • 1970-01-01
  • 2011-10-14
  • 1970-01-01
相关资源
最近更新 更多