WPF GridSplitter 不工作

WPF GridSplitter not working

我无法让 Gridsplitter 使用以下示例代码运行。网格拆分器不会移动或调整周围 "Top" 和 "Buttom" 网格行的大小,这些行设置为填充可用 space:

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

只需删除无用的网格:

<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 必须与您要调整大小的控件处于同一级别。

编辑: 正如 Clemens 所说,您的 GridSplitter 必须是您要拆分的网格的直接子项。当您这样做时,您正在将一个新的网格放入父网格的第 1 行:

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

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

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