XAML TabControl 边框问题

XAML TabControl Border Issues

我正在尝试制作支持滚动但保持 TabControl 的原始外观和感觉的自定义 TabControl,显然除了滚动之外。

首先,我选择编辑 TabControl 使用的原始模板的副本。

然后我在 TabPanel 周围放了一个 ScrollViewer。 However, this has caused a minor issue where the tabs now have a border at the bottom of them when they are selected.这可以在下面通过比较图像中的正常 TabControl 和样式 TabControl 看出。

起初我假设这是滚动查看器的 z 索引,但在尝试不同的值并确保滚动查看器的 z 索引和 TabPanel 都明显高于 Border的 z 索引,没有区别。

如果所选选项卡的底部没有边框,而它被包裹在 ScrollViewer 中,我如何才能达到相同的效果?


MainWindow.xaml

<Window x:Class="ScrollableTabControl.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <SolidColorBrush x:Key="TabItem.Selected.Background" Color="#FFFFFF"/>
        <SolidColorBrush x:Key="TabItem.Selected.Border" Color="#ACACAC"/>
        <Style x:Key="TabControlStyle1" TargetType="{x:Type TabControl}">
            <Setter Property="Padding" Value="2"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Background" Value="{StaticResource TabItem.Selected.Background}"/>
            <Setter Property="BorderBrush" Value="{StaticResource TabItem.Selected.Border}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabControl}">
                        <Grid x:Name="templateRoot" ClipToBounds="true" SnapsToDevicePixels="true" 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>
                            <ScrollViewer VerticalScrollBarVisibility="Disabled"
                                          HorizontalScrollBarVisibility="Disabled"
                                          Grid.Column="0"
                                          Grid.Row="0"
                                          Panel.ZIndex="1"
                                          Background="Transparent">
                                <TabPanel IsItemsHost="true"
                                          Margin="2,2,2,0"
                                          Panel.ZIndex="2"
                                          Background="Transparent"
                                          KeyboardNavigation.TabIndex="1"
                                          x:Name="headerPanel"/>
                            </ScrollViewer>
                            <Border x:Name="contentPanel"
                                    BorderBrush="{TemplateBinding BorderBrush}"
                                    BorderThickness="{TemplateBinding BorderThickness}"
                                    Background="{TemplateBinding Background}"
                                    Grid.Column="0"
                                    Panel.ZIndex="0"
                                    KeyboardNavigation.DirectionalNavigation="Contained"
                                    Grid.Row="1"
                                    KeyboardNavigation.TabIndex="2"
                                    KeyboardNavigation.TabNavigation="Local">
                                <ContentPresenter x:Name="PART_SelectedContentHost"
                                                  ContentSource="SelectedContent"
                                                  Margin="{TemplateBinding Padding}"
                                                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <TabControl Margin="5"
                    Grid.Row="0">
            <TabItem Header="Tab 1"/>
            <TabItem Header="Tab 2"/>
            <TabItem Header="Tab 3"/>
        </TabControl>
        <TabControl Margin="5"
                    Grid.Row="1"
                    Style="{DynamicResource TabControlStyle1}">
            <TabItem Header="Tab 1"/>
            <TabItem Header="Tab 2"/>
            <TabItem Header="Tab 3"/>
        </TabControl>
    </Grid>
</Window>

您可以试试这个以获得带有蓝色背景的自定义圆形标签 至于滚动,scrollviewer 应该做的工作

<TabControl.Resources>
            <Style TargetType="TabItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="TabItem">
                            <Border Name="Border" BorderThickness="1,1,1,1" CornerRadius="4,4,0,0" Margin="2,0" Background="#252e37">
                                <ContentPresenter x:Name="ContentSite"
                                    VerticalAlignment="Center"
                                    HorizontalAlignment="Center"
                                    ContentSource="Header"
                                    Margin="10,2"/>
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter TargetName="Border" Property="Background" Value="LightSkyBlue" />
                                </Trigger>
                                <Trigger Property="IsSelected" Value="False">
                                    <Setter TargetName="Border" Property="Background" Value="GhostWhite" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </TabControl.Resources>

因此,如果我们去查看 ScrollViewer style template,请注意其中有一个 Border,并为其设置了颜色,这就是您所看到的人工制品。

我们可以进入并编辑 ScrollViewer 的 Style 模板并将其删除....或者对于这个实例,我们可以让它保留其 Border 并覆盖样式继承,因此在您的模板中您可以执行类似的操作;

 <ScrollViewer ...>
    <ScrollViewer.Resources>
      <Color x:Key="BorderMediumColor">#FFFFFFFF</Color>
    </ScrollViewer.Resources>
 ....
 </ScrollViewer>

其中它应该继承 Border 的新颜色,在本例中我只是将其设为白色,或者您可以将 alpha 通道更改为“00”,这样它就变得透明了。或者您可以执行前面提到的并定义一个没有硬编码边框值的新样式模板。

希望这对您有所帮助,干杯!

附录:如果你找不到导致视觉边界线的罪魁祸首,你总是可以在 DOM 中对元素的布局进行一些欺骗,并利用边距覆盖线条并实现相同的期望视觉结果。这条线在技术上可能仍然存在,但它不存在的错觉仍然足够。 :)


工作代码示例

<Window x:Class="ScrollableTabControl.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <SolidColorBrush x:Key="TabItem.Selected.Background" Color="#FFFFFF"/>
        <SolidColorBrush x:Key="TabItem.Selected.Border" Color="#ACACAC"/>
        <Style x:Key="TabControlStyle1" TargetType="{x:Type TabControl}">
            <Setter Property="Padding" Value="2"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Background" Value="{StaticResource TabItem.Selected.Background}"/>
            <Setter Property="BorderBrush" Value="{StaticResource TabItem.Selected.Border}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabControl}">
                        <Grid x:Name="templateRoot"
                              ClipToBounds="true"
                              SnapsToDevicePixels="true"
                              KeyboardNavigation.TabNavigation="Local"
                              UseLayoutRounding="True"> <!-- Gets rid of pixel rounding errors which cause small bugs when window is a certain size -->
                            <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>
                            <ScrollViewer VerticalScrollBarVisibility="Auto"
                                          HorizontalScrollBarVisibility="Auto"
                                          Grid.Column="0"
                                          Grid.Row="0"
                                          Panel.ZIndex="1"
                                          Margin="0, 0, 0, -1.25"
                                          Background="Transparent"> <!-- +- 1.25 seems to be required when mixed with the ZIndex to hide the border underneath the selected tab -->
                                <TabPanel IsItemsHost="true"
                                          Margin="2,2,2,1.25"
                                          Background="Transparent"
                                          KeyboardNavigation.TabIndex="1"
                                          x:Name="headerPanel"/>
                            </ScrollViewer>
                            <Border x:Name="contentPanel"
                                    BorderBrush="{TemplateBinding BorderBrush}"
                                    BorderThickness="{TemplateBinding BorderThickness}"
                                    Background="{TemplateBinding Background}"
                                    Grid.Column="0"
                                    KeyboardNavigation.DirectionalNavigation="Contained"
                                    Grid.Row="1"
                                    KeyboardNavigation.TabIndex="2"
                                    KeyboardNavigation.TabNavigation="Local">
                                <ContentPresenter x:Name="PART_SelectedContentHost"
                                                  ContentSource="SelectedContent"
                                                  Margin="{TemplateBinding Padding}"
                                                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <TabControl Margin="5"
                    Grid.Row="0">
            <TabItem Header="Tab 1"/>
            <TabItem Header="Tab 2"/>
            <TabItem Header="Tab 3"/>
        </TabControl>
        <TabControl Margin="5"
                    Grid.Row="1"
                    Style="{DynamicResource TabControlStyle1}">
            <TabItem Header="Tab 1"/>
            <TabItem Header="Tab 2"/>
            <TabItem Header="Tab 3"/>
        </TabControl>
    </Grid>
</Window>