Skip to main content
 首页 » 编程设计

c#之如何在滚动的 WPF TabControl 中更改 TabItems 的外观

2024年02月24日24lovecherry

我有一个滚动的 TabControl,它使用 ScrollViewer 和 StackPanel(StackPanel 设置为 IsItemsHost="true")。首先,我正在使用最初在此处概述的解决方案 - Creating Scrolling Tabs Using WPF's TabControl .目前它有断开的链接(编辑:我已经在此处的论坛中找到了他的代码的一个实例 - How to prevent TabControl from doing multi rows?),所以这里是 TabControl 的 xaml(不需要任何进一步的代码):

<TabControl x:Name="TabControl2" Height="Auto" TabStripPlacement="Bottom" VerticalAlignment="Bottom" Template="{DynamicResource TabControlControlTemplate1}" IsSynchronizedWithCurrentItem="True"> 
  <TabControl.Resources> 
    <Style x:Key="TabScrollerRepeatButtonStyle" TargetType="{x:Type RepeatButton}"> 
      <Setter Property="Template"> 
        <Setter.Value> 
          <ControlTemplate> 
            <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1" Margin="1,0"> 
              <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Content="{TemplateBinding ContentControl.Content}"/> 
            </Border> 
          </ControlTemplate> 
        </Setter.Value> 
      </Setter> 
    </Style> 
 
    <ControlTemplate x:Key="TabControlControlTemplate1" TargetType="{x:Type TabControl}"> 
      <Grid x:Name="Grid" KeyboardNavigation.TabNavigation="Local"> 
        <Grid.ColumnDefinitions> 
          <ColumnDefinition x:Name="ColumnDefinition0"/> 
          <ColumnDefinition x:Name="ColumnDefinition1" Width="0"/> 
        </Grid.ColumnDefinitions> 
        <Grid.RowDefinitions> 
          <RowDefinition x:Name="RowDefinition0" Height="Auto"/> 
          <RowDefinition x:Name="RowDefinition1" Height="*"/> 
        </Grid.RowDefinitions> 
        <Border Grid.Row="1" Grid.Column="0" x:Name="ContentPanel" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0,0,1,1" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local" KeyboardNavigation.DirectionalNavigation="Contained"> 
          <Border x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> 
            <Border Background="{TemplateBinding Background}" x:Name="Border1"> 
              <ContentPresenter DataContext="{x:Null}" Margin="{TemplateBinding Padding}" x:Name="PART_SelectedContentHost" Content="{TemplateBinding SelectedContent}" ContentTemplate="{TemplateBinding SelectedContentTemplate}" ContentTemplateSelector="{TemplateBinding SelectedContentTemplateSelector}" ContentSource="SelectedContent"/> 
            </Border> 
          </Border> 
        </Border> 
        <ScrollViewer x:Name="HeaderPanel" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,0,0,0" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled"> 
          <ScrollViewer.Style> 
            <Style TargetType="{x:Type ScrollViewer}"> 
              <Setter Property="Template"> 
                <Setter.Value> 
                  <ControlTemplate> 
                    <Grid Margin="0,0,0,0" Grid.Row="0" Grid.Column="0" x:Name="HeaderPanel"> 
                      <Grid.ColumnDefinitions> 
                        <ColumnDefinition Width="20"/> 
                        <ColumnDefinition Width="20"/> 
                        <ColumnDefinition Width="*"/> 
                        <ColumnDefinition Width="20"/> 
                      </Grid.ColumnDefinitions> 
                      <Grid.RowDefinitions> 
                        <RowDefinition Height="Auto"/> 
                      </Grid.RowDefinitions> 
                      <RepeatButton Grid.Column="1" Content="&lt;" Command="ScrollBar.LineLeftCommand" Style="{DynamicResource TabScrollerRepeatButtonStyle}" Visibility="{Binding Path=ComputedHorizontalScrollBarVisibility, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"/> 
                      <ScrollContentPresenter Grid.Column="2" Content="{TemplateBinding ScrollViewer.Content}" /> 
                      <RepeatButton Grid.Column="3" Content="&gt;" Command="ScrollBar.LineRightCommand" Style="{DynamicResource TabScrollerRepeatButtonStyle}" Visibility="{Binding Path=ComputedHorizontalScrollBarVisibility, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"/> 
                    </Grid> 
                  </ControlTemplate> 
                </Setter.Value> 
              </Setter> 
            </Style> 
          </ScrollViewer.Style> 
          <StackPanel IsItemsHost="true" Orientation="Horizontal" Background="{x:Null}" KeyboardNavigation.TabIndex="1"/> 
        </ScrollViewer> 
      </Grid> 
    </ControlTemplate> 
  </TabControl.Resources> 
  <TabItem x:Name="TabItem1" Header="TabItem1"/> 
  <TabItem x:Name="TabItem2" Header="TabItem2"/> 
</TabControl> 

如何调整每个 TabItem 的外观?例如,我想在 StackPanel 的帮助下在每个 TabItem 中放置一个 TextBox 和 TextBlock,这样我就可以拥有可重命名的选项卡(根据需要折叠一个或另一个)。我可能还想在每个选项卡上添加一个关闭按钮。通常,我会使用如下内容:

                <TabControl.ItemTemplate> 
                    <DataTemplate> 
                        <StackPanel Name="panel" Orientation="Horizontal"> 
                            <TextBox   Name="editHeader" Text="{Binding Header}" MaxWidth="250"/> 
                            <TextBlock Name="textHeader" Text="{Binding Header}" /> 
                        </StackPanel> 
                    </DataTemplate> 
                </TabControl.ItemTemplate> 

.. 但这根本没有效果。任何想法,将不胜感激。谢谢。

编辑:我仍在努力解决这个问题。解决方案是否可能涉及 ContentPresenter 和/或 SelectedContentTemplate?

编辑 2:(这不会增加我的问题的值(value))我真的非常希望 WPF 包含这种开箱即用的东西。我对 TabControl 的默认行为感到困惑,而且几年后没有可滚动的 TabControl(也没有简单的解决方案来实现)这一事实。

请您参考如下方法:

嘿猜猜。我做过类似的事情,不过我的想法是基于这个系列

http://www.blogs.intuidev.com/post/2010/post/2010/01/25/TabControlStyling_PartOne.aspx

使用的方法是简单地设置tabitems模板的样式。例如:

<TabControl.Resources> 
    <Style TargetType="{x:Type TabItem}"> 
        <Setter Property="Template"> 
            <Setter.Value> 
                <ControlTemplate TargetType="{x:Type TabItem}"> 
                <!-- your custom template goes here --> 
 
                </ControlTemplate> 
            </Setter.Value> 
        </Setter> 
    </Style> 
</TabControl.Resources> 

不幸的是,这种方法意味着您将不得不完全重新定义选项卡项的外观和行为方式。但所提供的链接提供了有关如何实现这一目标的很好示例。

希望对您有所帮助。