WPF GridSplitter无法正常工作

时间:2018-07-31 14:16:55

标签: c# wpf gridsplitter

我无法使用以下示例代码来使Gridsplitter起作用。网格分割器不会移动或调整设置为填充可用空间的周围“顶部”和“底部”网格行的大小或大小:

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

    <Grid Grid.Row="0">
        <TextBlock FontSize="55" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap">Top</TextBlock>
    </Grid>

    <Grid Grid.Row="1">
        <GridSplitter Height="5" HorizontalAlignment="Stretch" ResizeDirection="Rows"/>
    </Grid>

    <Grid Grid.Row="2">
        <TextBlock FontSize="55" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap">Bottom</TextBlock>
    </Grid>
</Grid>

2 个答案:

答案 0 :(得分:1)

只需删除无用的网格:

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

        <TextBlock FontSize="55" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap">Top</TextBlock>

        <GridSplitter Grid.Row="1" Height="5" HorizontalAlignment="Stretch" ResizeDirection="Rows"/>

        <TextBlock FontSize="55"  Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap">Bottom</TextBlock>
</Grid>

编辑:

为清楚起见:GridSplitter控件仅调整Grid子层次结构中相同级别的元素的大小。您可以将所需的任何内容放入网格中,但必须将GridSplitter置于要调整大小的控件的同一级别。

您仍然可以这样做:

<Grid Width="Auto" Height="Auto">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid>
        <TextBlock FontSize="55" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap">Top</TextBlock>
    </Grid>

    <GridSplitter Grid.Row="1" Height="5" HorizontalAlignment="Stretch" ResizeDirection="Rows"/>

    <Grid Grid.Row="2">
      <TextBlock FontSize="55"  HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap">Bottom</TextBlock>
    </Grid>
</Grid>

但是GridSplitter必须与您要调整大小的控件处于同一级别。

答案 1 :(得分:1)

编辑:正如Clemens所说,您的GridSplitter必须是要拆分的网格的直接子代。您将在执行以下操作时将新的网格放入父网格的第1行:

<Grid Grid.Row="1">
    <GridSplitter Height="5" HorizontalAlignment="Stretch" ResizeDirection="Rows"/>
</Grid>

您需要将拆分器直接放入要拆分的父网格中,并在element标签中声明该行:

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

       <TextBlock Grid.Row="0" FontSize="55" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap">Top</TextBlock>

       <GridSplitter Grid.Row="1" Height="5" HorizontalAlignment="Stretch" ResizeDirection="Rows"/>

       <TextBlock Grid.Row="2" FontSize="55" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap">Bottom</TextBlock>
</Grid>