使用XAML如何向TabItem添加列

时间:2017-01-31 21:02:18

标签: wpf xaml

如何向TabItem添加列,以便我可以使用拆分器调整列的大小并同时在每列中堆叠控件?

我创建了以下XAML。我认为随着分割器的大小调整,按钮会随每列重新调整大小。

    <Grid Background="LightBlue">
      <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
      </Grid.RowDefinitions>
      <DockPanel Grid.Row="0" >
        <StackPanel>
          <TextBlock Text="Multiple items go here as the header" />
        </StackPanel>
      </DockPanel>
      <TabControl Grid.Row="1" Background="Beige" >
        <TabItem x:Name="Tab1" Header="Tab One" >
          <DockPanel>
            <StackPanel DockPanel.Dock="Top">
              <TextBlock Text="Header for this tab goes here" />
            </StackPanel>
            <Grid>
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="100" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
              </Grid.ColumnDefinitions>
              <GridSplitter Grid.Column="1" VerticalAlignment="Stretch"
                            Background="Aqua" Width="4" />
              <GridSplitter Grid.Column="3" VerticalAlignment="Stretch"
                            Background="Aqua" Width="4" />
              <StackPanel Grid.Column="0" >
                <Button Content="Column 1" />
              </StackPanel>
              <StackPanel Grid.Column="2" >
                <Button Content="Column 2" />
              </StackPanel>
              <StackPanel Grid.Column="4" >
                <Button Content="Column 3" />
              </StackPanel>
            </Grid>
          </DockPanel>
        </TabItem>
        <TabItem x:Name="Tab2" Header="Tab Two" ></TabItem>
        <TabItem x:Name="Tab3" Header="Tab Three" ></TabItem>
      </TabControl>
    </Grid>

1 个答案:

答案 0 :(得分:1)

只需将ResizeBehavior="PreviousAndNext"添加到GridSplitters即可。

GridSplitter.ResizeBehavior Property的默认值为GridResizeBehavior.BasedOnAlignment,表示

  

根据Horizo​​ntalAlignment和VerticalAlignment属性的值重新分配空间。

无论那意味着什么。但你想要PreviousAndNext意味着

  

对于水平GridSplitter,在为GridSplitter指定的行的上方和下方的行之间重新分配空间。对于垂直GridSplitter,在为GridSplitter指定的列的左侧和右侧的列之间重新分配空间。