网格/数据网格不会扩展到空间

时间:2016-04-28 08:46:44

标签: wpf layout visibility

我有一个包含4行的网格2包含datagrids。我想基于ShowPackages复选框显示/隐藏一个数据网格。代码都正常工作,并显示/隐藏数据网格。但是,问题是第二行中的其他数据网格不会扩展到可用空间。

我理解这可能与定义4行以及所有这些渲染到所述空间有关。如何实现所需的功能?

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

2 个答案:

答案 0 :(得分:1)

第二个RowDefinition高度必须是auto:

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

答案 1 :(得分:0)

更改为:

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