如何:使用 GridSplitter 调整行大小

此示例演示如何使用水平 GridSplitter 重新分配两 Grid 行之间的空间,而无需更改维度 Grid

示例:

如何创建覆盖行边缘的 GridSplitter

若要在 Grid 中指定用于调整相邻行大小的 GridSplitter,请将 Row 附加属性设置为您希望调整大小的某一行。 Grid如果有多个列,请设置ColumnSpan附加属性以指定列数。 然后将VerticalAlignment设置为TopBottom(你设置的对齐方式取决于要调整大小的两行)。 最后,将 HorizontalAlignment 属性设置为 Stretch.

以下示例演示如何定义调整相邻行大小的水平 GridSplitter

<GridSplitter Grid.Row="1" 
              Grid.ColumnSpan="3" 
              HorizontalAlignment="Stretch" 
              VerticalAlignment="Top"
              Background="Black" 
              ShowsPreview="true"
              ResizeDirection="Rows"
              Height="5"/>

不占用其自己的行的一个 GridSplitter 控件可能会被其中 Grid的其他控件遮盖。 有关如何防止此问题的详细信息,请参阅 确保 GridSplitter 可见

如何创建在行中占位的 GridSplitter

若要指定在Grid中占据一行的GridSplitter,请将Row附加属性设置为要调整大小的其中一行。 Grid如果有多个列,请将ColumnSpan附加属性设置为列数。 然后将VerticalAlignment设置为Center,将HorizontalAlignment属性设置为Stretch,并将包含GridSplitter的行的Height设置为Auto

下面的示例演示如何定义一个水平的 GridSplitter,它占据一行,并调整其两侧行的大小。

<Grid.RowDefinitions>
  <RowDefinition Height="50*" />
  <RowDefinition Height="Auto" />
  <RowDefinition Height="50*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Grid.Column="1" Background="Tan"/>
<GridSplitter Grid.Row="1"
              HorizontalAlignment="Stretch"
              VerticalAlignment="Center"
              Background="Black" 
              ShowsPreview="True"
              Height="5"
           />
<StackPanel Grid.Row="2" Grid.Column="0" Background="Brown"/>

另请参阅