如何让 Grid 填满 window?

How to make Grid fill the window?

现在我有一个网格定义如下,

<Grid Background="Red">
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="50" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid Grid.Row="0" x:Name="Grid1">
        <ListBox>
            <ListBoxItem>Item 1</ListBoxItem>
            <ListBoxItem>Item 2</ListBoxItem>
            <ListBoxItem>Item 3</ListBoxItem>
            <ListBoxItem>Item 4</ListBoxItem>
            <ListBoxItem>Item 5</ListBoxItem>
            <ListBoxItem>Item 6</ListBoxItem>
            <ListBoxItem>Item 7</ListBoxItem>
            <ListBoxItem>Item 8</ListBoxItem>
            <ListBoxItem>Item 9</ListBoxItem>
            <ListBoxItem>Item 10</ListBoxItem>
        </ListBox>
    </Grid>
    <Label Grid.Row="1" Background="Gray" Content="123"/>
    <Grid Grid.Row="2" x:Name="Grid2" Visibility="Visible">
        <ListBox>
            <ListBoxItem>Item 1</ListBoxItem>
            <ListBoxItem>Item 2</ListBoxItem>
            <ListBoxItem>Item 3</ListBoxItem>
            <ListBoxItem>Item 4</ListBoxItem>
            <ListBoxItem>Item 5</ListBoxItem>
            <ListBoxItem>Item 6</ListBoxItem>
            <ListBoxItem>Item 7</ListBoxItem>
            <ListBoxItem>Item 8</ListBoxItem>
            <ListBoxItem>Item 9</ListBoxItem>
            <ListBoxItem>Item 10</ListBoxItem>
        </ListBox>
    </Grid>
</Grid>

Grid1Grid2身高相同

当我将Grid2的Visibility设为Collapsed时,Label下方会出现空白区域。

如何去除这个空白区域?

应该是:

您可以使用 DataTrigger.

Grid2 元素的 Visibility 特征设置为第三行的高度
<Grid Background="Red">
  <Grid.RowDefinitions>
    <RowDefinition Height="*" />
    <RowDefinition Height="50" />
    <RowDefinition>
      <RowDefinition.Style>
        <Style TargetType="{x:Type RowDefinition}">
          <Setter Property="Height" Value="*" />
             <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Visibility,ElementName=Grid2}" Value="Hidden">
                    <Setter Property="Height" Value="0" />        
                </DataTrigger>
              </Style.Triggers>
        </Style>
      </RowDefinition.Style>
    </RowDefinition>
  </Grid.RowDefinitions>
  <Grid Grid.Row="0" x:Name="Grid1">
    <ListBox>
      <ListBoxItem>Item 1</ListBoxItem>
      <ListBoxItem>Item 2</ListBoxItem>
      <ListBoxItem>Item 3</ListBoxItem>
      <ListBoxItem>Item 4</ListBoxItem>
      <ListBoxItem>Item 5</ListBoxItem>
      <ListBoxItem>Item 6</ListBoxItem>
      <ListBoxItem>Item 7</ListBoxItem>
      <ListBoxItem>Item 8</ListBoxItem>
      <ListBoxItem>Item 9</ListBoxItem>
      <ListBoxItem>Item 10</ListBoxItem>
    </ListBox>
  </Grid>
  <Label Grid.Row="1" Background="Gray" Content="123" />
  <Grid Grid.Row="2" x:Name="Grid2" Visibility="Hidden">
    <ListBox>
      <ListBoxItem>Item 1</ListBoxItem>
      <ListBoxItem>Item 2</ListBoxItem>
      <ListBoxItem>Item 3</ListBoxItem>
      <ListBoxItem>Item 4</ListBoxItem>
      <ListBoxItem>Item 5</ListBoxItem>
      <ListBoxItem>Item 6</ListBoxItem>
      <ListBoxItem>Item 7</ListBoxItem>
      <ListBoxItem>Item 8</ListBoxItem>
      <ListBoxItem>Item 9</ListBoxItem>
      <ListBoxItem>Item 10</ListBoxItem>
    </ListBox>
  </Grid>
</Grid>