4

我正在使用 GridSplitter 来调整网格中单元格的大小,但是它的行为不是我所期望的,我找不到解决方案。它是一个三行网格,第一行的行定义设置为 Auto 并包含一些元素。第二行有一些数据,并有一个行定义 * 来填充剩余空间。最后一行是需要调整大小的状态栏,因此其中有一个网格拆分器,行定义高度为 Auto 和 MinHeight 为 30。

问题是当您将 GridSplitter 一直拖到顶部时,它会使单元格溢出。我希望它一旦到达顶部就停止。可以通过从最后一行删除 Height=Auto 来实现所需的行为,但这会使底部单元格扩展到与中间行相等的高度。

这是一个 XAML Pad 示例。

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    <Grid ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" MinHeight="20" />
            <RowDefinition Height="Auto" MinHeight="30" />
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0" Text="Foo" />
        <TextBlock Grid.Row="1" Text="Bar" />
        <GridSplitter  Canvas.ZIndex="1"  VerticalAlignment="Top"  Grid.Row="2" Background="Cyan" Height="5" HorizontalAlignment="Stretch"  />
        <TextBlock VerticalAlignment="Bottom" Grid.Row="2" TextWrapping="Wrap">LOL<LineBreak/>LOL<LineBreak/>LOL</TextBlock>
    </Grid>
</Page>

当您拖动到顶部时,您会注意到底部的文本消失了。

我尝试了各种方法,例如将网格拆分器放在它自己的单元格中,以及将高度绑定到另一个对象 ActualHeight 等,但没有一个真的能很好地工作。

我知道这不是解释得最好的问题,但任何想法都会非常感激。

编辑: 我已经用自己的行制作了 GridSplitter,如下所示,但正如我之前提到的,问题仍然存在。我在这里也设置了 ResizeBehavior 和 ResizeDirection。

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    <Grid ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" MinHeight="20" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" MinHeight="30"  />
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0" Text="Foo" />
        <TextBlock Grid.Row="1" Text="Bar" />
        <GridSplitter ResizeDirection="Rows" ResizeBehavior="PreviousAndNext" Grid.Row="2" Background="Cyan" Height="5" HorizontalAlignment="Stretch"  />
        <TextBlock VerticalAlignment="Bottom" Grid.Row="3" TextWrapping="Wrap">LOL<LineBreak/>LOL<LineBreak/>LOL</TextBlock>
    </Grid>
</Page>

一个有效的例子是删除最后一行 Height="Auto" 并将其更改为 * 像这样

然而,这使得最后一行的大小等于它之前的行,而不是请求的单元格大小。

4

2 回答 2

3

GridSplitter应该位于它自己的行或列。试用 GridSplitter.ResizeDirection和GridSplitter.ResizeBehavior属性

看看以下文章:

更新

您可以向对象提供“星系数” GridLength。例如:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:sys="clr-namespace:System;assembly=mscorlib" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">   
    <Grid ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="95*" MinHeight="20" /> <!--here we are using 95*-->
            <RowDefinition Height="Auto" />
            <RowDefinition Height="5*" MinHeight="30"/> <!--and here we are using 5*-->     
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0" Text="Foo" />
        <TextBlock Grid.Row="1" Text="Bar" />
        <GridSplitter ResizeDirection="Rows"  Grid.Row="2" Background="Cyan" Height="5" HorizontalAlignment="Stretch"  />
        <TextBlock VerticalAlignment="Bottom" Grid.Row="3" TextWrapping="Wrap">LOL<LineBreak/>LOL<LineBreak/>LOL</TextBlock>
    </Grid> 
</Page>

所以我们有你需要的布局,没有GridSplitter不清楚的行为。

于 2010-09-30T07:39:58.067 回答
1

德拉特,打败我。我不妨张贴我所拥有的。您的问题与第三行定义有关。当您开始向上滚动并且文本消失时,行的高度会不断增加。如果 Eugene 的解决方案不起作用,您可以尝试将最大高度设置为某种限制。

于 2010-09-30T07:55:48.777 回答