在不切断选项卡的情况下为WPF TabItem添加水平边距,可以通过以下方法实现:
<TabControl Margin="10,0,10,0">
<TabItem Header="TabItem1">
<!-- 内容 -->
</TabItem>
<TabItem Header="TabItem2">
<!-- 内容 -->
</TabItem>
</TabControl>
<TabControl>
<TabItem>
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Margin="10,0,0,0" Text="TabItem1"/>
</StackPanel>
</TabItem.Header>
<!-- 内容 -->
</TabItem>
<TabItem>
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Margin="10,0,0,0" Text="TabItem2"/>
</StackPanel>
</TabItem.Header>
<!-- 内容 -->
</TabItem>
</TabControl>
<TabControl>
<TabControl.Resources>
<Style TargetType="TabItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
<Grid>
<Border Name="Border" Margin="10,0,10,0" BorderThickness="1,1,1,0" CornerRadius="4,4,0,0" Background="{TemplateBinding Background}">
<ContentPresenter ContentSource="Header" Margin="10,5,10,5"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlLightLightBrushKey}}"/>
<Setter TargetName="Border" Property="BorderThickness" Value="1,1,1,0"/>
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="Border" Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
<Setter TargetName="Border" Property="BorderThickness" Value="1,1,1,0"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</TabControl.Resources>
<TabItem Header="TabItem1">
<!-- 内容 -->
</TabItem>
<TabItem Header="TabItem2">
<!-- 内容 -->
</TabItem>
</TabControl>
以上三种方法均可实现在不切断选项卡的情况下为WPF TabItem添加水平边距。
领取专属 10元无门槛券
手把手带您无忧上云