Grid/Datagrid 未扩展到 space

Grid/Datagrid not expanding into space

我有一个包含 4 行的网格,其中 2 行包含数据网格。我想 show/hide 一个基于 ShowPackages 复选框的数据网格。代码全部正常工作并且是 showing/hiding 数据网格。但是,问题是第二行中的另一个数据网格没有扩展到可用 space。

我知道这可能与定义 4 行和所有这些呈现到 space 中有关。我如何实现所需的功能?

   <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal">
            <CheckBox Content="Show Completed" IsChecked="{Binding ShowCompletedCommand}" Margin="3"/>
            <CheckBox Content="Show Packages" IsChecked="{Binding ShowPackages}" Margin="3"/>
        </StackPanel>    
        <DataGrid Grid.Row="1" ItemsSource="{Binding WorkOrders}" SelectedValue="{Binding SelectedWorkOrder}" AutoGenerateColumns="True"/>        
        <DataGrid Grid.Row="2" Visibility="{Binding ShowPackages, Converter={StaticResource BoolToVis}}" ItemsSource="{Binding Packages}" />
        <Grid Grid.Row="3">
            <StackPanel Orientation="Horizontal">
                <Button Command="{Binding RefreshCommand}" Content="Refresh"/>
                <Button Command="{Binding CancelCommand}" Content="Cancel"/>
            </StackPanel>
     </Grid>

更改为:

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

第二个 RowDefinition 高度必须是自动的:

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

编辑。这种方法应该有效

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition>
            <RowDefinition.Style>
                <Style TargetType="RowDefinition">
                    <Setter Property="Height" Value="*"></Setter>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=PackagesCB, Path=IsChecked}" Value="False">
                            <Setter Property="Height" Value="0" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </RowDefinition.Style>
        </RowDefinition>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <StackPanel Orientation="Horizontal">
        <CheckBox Content="Show Completed" IsChecked="{Binding ShowCompletedCommand}" Margin="3"/>
        <CheckBox x:Name="PackagesCB" Content="Show Packages" IsChecked="{Binding ShowPackages}" Margin="3"/>
    </StackPanel>
    <DataGrid Grid.Row="1" ItemsSource="{Binding WorkOrders}" SelectedValue="{Binding SelectedWorkOrder}" AutoGenerateColumns="True"/>
    <DataGrid Grid.Row="2" ItemsSource="{Binding Packages}"/>
    <Grid Grid.Row="3">
        <StackPanel Orientation="Horizontal">
            <Button Command="{Binding RefreshCommand}" Content="Refresh"/>
            <Button Command="{Binding CancelCommand}" Content="Cancel"/>
        </StackPanel>
    </Grid>
</Grid>